note 40331 deleted from features.http-auth by didou
| From: | didou@php.net | Date: | Sun, 29 Feb 2004 13:47:56 +0000 |
| Subject: | note 40331 deleted from features.http-auth by didou | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-65991@lists.php.net to get a copy of this message | ||
Note Submitter: axiom at vectorns dot com
----
I could not get any of these ways of authenticating to a database to work for me so here is a script
that I wrote from scratch that authenticates to a sql database and then does something based on
that.
<?php
$username = $_SERVER['PHP_AUTH_USER'];
$password = $_SERVER['PHP_AUTH_PW'];
if (!isset($_SERVER['PHP_AUTH_USER'])) {
header('WWW-Authenticate: Basic realm="My Realm"');
header('HTTP/1.0 401 Unauthorized');
echo 'Text to send if user hits Cancel button';
exit;
} else {
$link = mysql_connect('localhost','test','test');
mysql_select_db('users');
$query = "select username, password from users where username = '$username' and
password = '$password'";
$result = mysql_query($query);
if (($value = mysql_fetch_row($result)) && ($value[0] == $username) && ($value[1] ==
$password)) {
echo "You are authenticated";
} else {
echo "Your username or password is incorrect";
}
echo "<p>Hello {$_SERVER['PHP_AUTH_USER']}.</p>";
echo "<p>You entered {$_SERVER['PHP_AUTH_PW']} as your
password.</p>";
}
mysql_close($link);
?>