If you are looking for the easiest way to protect your web page with a password using PHP code please find the solution here:
http://websites-development.com/blog/simplest-php-login
If you need to use this functionality on a FastCGI PHP server then read below.
When using Apache’s Basic Auth with PHP running in FastCGI Mode, the credentials of the User do not get passed to the PHP ($_SERVER['PHP_AUTH_USER'] and $_SERVER['PHP_AUTH_PW'] are not set and headers look a little different).
To make it work on FastCGI this trick is needed:
1. Create a .htaccess file and add these lines:
RewriteEngine on
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
2. Add the following code before checking the credentials in your PHP file:
//4fastcgi
if(isset($_SERVER['HTTP_AUTHORIZATION'])) {
$auth_params = explode(":" , base64_decode(substr($_SERVER['HTTP_AUTHORIZATION'], 6)));
$_SERVER['PHP_AUTH_USER'] = $auth_params[0];
unset($auth_params[0]);
$_SERVER['PHP_AUTH_PW'] = implode('',$auth_params);
}
Since PHP v5.4 you can also use getallheaders() or apache_request_headers() functions (which became available under FastCGI) to get the values.