This class provides HTTP Basic authentication. Try it here
as user test with password pass.
It will be useful if you want to share the same protection system for a bunch of documents in a directory and a specific page located elsewhere. At the top of your page, include the following code; the constructor needs the path to the .htpasswd file:
include('./apacheAuthBasic.php');
$auth = new apacheAuthBasic('./.htpasswd');
$auth->checkAuthorized();
checkAuthorized() checks the user and password match. If they don't, it sends a request for authentication:
function checkAuthorized
() { if (!
$this->
okPasswd($this->
htpasswd)) { header('WWW-Authenticate: Basic realm="' .
$this->
realm .
'"');
header('HTTP/1.0 401 Unauthorized');
echo $this->
message;
die();
} }
As you can guess, okPasswd() compares the encrypted password stored in .htpasswd with the clear password from the browser. To do this, it first encrypts the clear password with makePasswd(). Ancillary function makeHtpasswd() returns the user:password combination in a suitable format for .htpasswd.
Download: apacheAuthBasic.zip.
Note: this code is compatible with the default CRYPT encryption. The MD5 encryption is more complex and is not supported.