httpAuthFilter - simple symfony backend login
credits James McGlinn
filters.yml
-----------
...
# generally, you will want to insert your own filters here
httpAuthFilter:
class: httpAuthFilter
...
app.yml
-------
all:
auth:
realm: Password Required
username: zlosyn
password: nysloz
apps/backend/lib/httpAuthFilter.class.php
------------------------
<?php
/**
* HTTP Authentication filter for Symfony
*
* http://blog.phpdeveloper.co.nz/2006/10/16/symfony-http-authentication-filter/
* @author James McGlinn
* @version 1
*/
class httpAuthFilter extends sfFilter
{
/**
* Execute filter
*
* @param sfFilterChain $filterChain
*/
public function execute ($filterChain)
{
// execute filter once
if ($this->isFirstCall()) {
if (!isset($_SERVER['PHP_AUTH_USER'])) {
$this->sendHeadersAndExit();
}
if (!($_SERVER['PHP_AUTH_USER'] == sfConfig::get('app_auth_username') && $_SERVER['PHP_AUTH_PW'] == sfConfig::get('app_auth_password'))) {
$this->sendHeadersAndExit();
}
}
// execute next filter
$filterChain->execute();
}
/**
* Sends HTTP Auth headers and exits
*
* @return null
*/
private function sendHeadersAndExit ()
{
header('WWW-Authenticate: Basic realm="' . sfConfig::get('app_auth_realm') . '"');
header('HTTP/1.0 401 Unauthorized');
exit;
}
}
Tagy:
php 64 řádků | 2008-02-06 18:06:16 | air.kadlec@seznam.cz