Trying to make a simple connection to a web page and have authentication via a MySQL Database. I could enter in all info via a command line with no problems. Could open MySQL, could select the database, run queries, update data, etc... However, whenever I tried to do this via PHP, this never worked. I was not getting any error messages (once I fixed other problems) and there was nothing in the logs. I continued to enter user name and password, however, the login dialog would just flash and then after 3 tries would go to my fail page. Finally, what I did was that I entered the user name and password, the dialog would flash and the fields would be empty. I then clicked CANCEL and I was then authenticated! I can reproduce this repeatedly. I have no idea why this works, but it does. It does this both on the Linux box (this is the one that I am working on) and from a Windows box opening the page with IE. Below is the authentication php code ::
<?php
$authorized = FALSE;
if (!$authorized) {
header('WWW-Authenticate: Basic Realm="My Site" ');
header('HTTP/1.0 401 Unauthorized');
}
if ( (isset($_SERVER[PHP_AUTH_USER]) AND isset($_SERVER[PHP_AUTH_PW])) ) {
$DB_HOST="localhost";
$DB_USER="user";
$DB_PASSWORD="pass";
$DB_NAME="faqDB";
//Set DB info
$dbc = @mysql_connect ($DB_HOST, $DB_USER, $DB_PASSWORD) OR die ('Could not connect to MySQL:' . mysql_error() );
mysql_select_db ($DB_NAME) OR die ('Could not select database:' . mysql_error() );
$query = "SELECT username, password FROM faqusers WHERE username='$_SERVER[PHP_AUTH_USER]' and password='$_SERVER[PHP_AUTH_PW]' ";
$query2 = "SELECT user_id FROM faqusers WHERE username='{$_SERVER['PHP_AUTH_USER']}' AND password='{$_SERVER['PHP_AUTH_PW']}' ";
$result = mysql_query ($query);
$row = @mysql_fetch_array ($result);
if ($row) {
$authorized = TRUE;
}
// if ($authorized) {
// echo '<p>Result = ' . $row[0] . ' ' . $row[1] . '</p>';
// }
}
?>
Below is the code for the index.php page
<?php
require_once('..auth.php');
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"
http://www.w3.org/TR/2000/REC-xhtml1-20000126/DTD/xhtml1-transitional.dtd">
<html xmlns="
http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="content-type" content="text/html"; charset=iso-8859-1" />
<title>FAQs Home</title>
</head>
<body>
<table align="center">
<tr>
<td><img src="Header_1.jpg"</td>
</tr>
</table>
<?php
if (!$authorized) {
echo '<p>Please enter a valid user name and password! Click <a href="index.php">here</a> to try again.</p>' ;
} else {
echo '<p>Welcome <b>' . $_SERVER['PHP_AUTH_USER'] . '</b>, you have been authenticated to the FAQ Database!</p><br/>';
// echo '<p>Welcome <b> ' . $_SERVER['PHP_AUTH_USER'] . '</b> you are connected to <b>' . $DB_NAME . '</b> on <b>' . $DB_HOST .'</b> using password <b>' . $_SERVER['PHP_AUTH_PW'] . '!</b></p>';
}
?>
</body>
</html>
PHP Version is ::5.1.2-pl1-gentoo
MySQL Version is :: 5.0.19
Apache Version is :: 2.0.55
Any thoughts on this would be great.
Thanks ::
B