PHP / Authentication / Login Authenticate
Snippet details
ID: 87
Viewed: 3387
Added: 2004-06-24
Version: php4.1+
Sorry no demo
This brings up a password window where the user has to enter a username and password. it then checks with Mysql to see if the username and password are real (compares it to the original). it then will load a page for the user to see or it will bring it up the password box again.<br><br>put the following in a seperate php file and include it on all your protected files: <br>
this will not work work if your server is setup in cgi mode.
this will not work work if your server is setup in cgi mode.
General Details
Snippet uploaded by: snippet
Email : webmaster@snippetlibrary.com
Snippet By: snippet
<!---Head--->
none
<!---Body--->
<?php
if (!isset($_SERVER['PHP_AUTH_USER'])) {
header('WWW-Authenticate: Basic realm="Login"');
header('HTTP/1.0 401 Unauthorized');
exit;
} else if (isset($_SERVER['PHP_AUTH_USER'])) {
$sql = "SELECT * FROM users WHERE username='".htmlentities($_SERVER['PHP_AUTH_USER'])."' and password='".htmlentities($_SERVER['PHP_AUTH_PW'])."'";
$result = mysql_query($sql);
$num = mysql_num_rows($result);
if ($num != 1) {
header('WWW-Authenticate: Basic realm="Login"');
header('HTTP/1.0 401 Unauthorized');
exit;
} else{
// show page you want them to see here
}
}
?>
No Reviews to show
