Password Protect a Web Page
<?php
//
if(!isset($PHP_AUTH_USER))
{
Header("WWW-Authenticate: Basic realm=\"You need an user account to acces this website!\"");
Header("HTTP/1.0 401 Unauthorized");
echo "<a href=\"http://mywebsite.com\">Main Site</a>\n";
exit;
}
else
{
$user_passwords = array (
// user1 is the login name and password1
// is that users password. Add as many
// lines as you like.
"user1" => "password1",
"user2" => "password2",
"user3" => "password3",
"user4" => "password4"
);
if (($PHP_AUTH_USER == "") || ($PHP_AUTH_PW == ""))
{
Header("HTTP/1.0 401 Unauthorized");
echo "Sorry, could find your password!";
exit;
}
else if (strcmp($user_passwords[$PHP_AUTH_USER],$PHP_AUTH_PW) == 0)
{
// This is the page that should be password protected.
echo "This is the password protected page.";
exit;
}
else
{
Header("HTTP/1.0 401 Unauthorized");
echo "Sorry, could find your password!";
exit;
}
}
?>