PHP's HTTP authentication mechanism works only when PHP is running in Apache mode, so this feature does not apply to CGI versions. In the Apache module's PHP script, you can use the header () function to send "authentication Required" information to the client browser to eject a username/password input window. When the user enters the username and password, the PHP script containing the URL will be invoked again with the predefined variable php_auth_user, PHP_AUTH_PW, and Auth_type, which are set to the username, password, and authentication type respectively. Predefined variables are saved in an array of $_server or $HTTP _server_vars. The system only supports "basic" authentication
<?php Tutorial
$authorized = FALSE;
if (Isset ($_server[' Php_auth_user ')) && isset ($_server[' PHP_AUTH_PW ')) {
$authFile = File ("./password.txt");
foreach ($authFile as $login) {
List ($username, $password) = Explode (":", $login);
$password = Trim ($password);
if (($username = = $_server[' Php_auth_user ']) && ($ Password = = MD5 ($_server[' PHP_AUTH_PW '))) {
$authorized = TRUE;
break;
}
}
}
If not authorized, display authentication prompt or 401 error
if (! $authorized) {
Header (' Www-authenticate:basic realm= "Secret Stash");
Header (' http/1.0 401 Unauthorized ');
Print (' You must provide the proper credentials! ');
Exit
}
?>
<!--Password.txt
Joe:60d99e58d66a5e0f4f89ec3ddd1d9a80
-->