Hi, I’m trying to authenticate a user against a MySQL database and get the following errors. Note, I’m a beginner in the early stages and this is script from a book I’m reading. Any help would be greatly appreciated!
Thank you…
Error:
Warning: mysql_query(): supplied argument is not a valid MySQL-Link resource in C:\xampp\htdocs\test\passwordtest2.php on line 27
Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in C:\xampp\htdocs\test\passwordtest2.php on line 30
Warning: Cannot modify header information - headers already sent by (output started at C:\xampp\htdocs\test\passwordtest2.php:27) in C:\xampp\htdocs\test\passwordtest2.php on line 6
Warning: Cannot modify header information - headers already sent by (output started at C:\xampp\htdocs\test\passwordtest2.php:27) in C:\xampp\htdocs\test\passwordtest2.php on line 7
My Database:
authenticationtest
My authentication table settings are as follows:
rowID TINYINT UNSIGNED NOT NULL AUTO_INCREMENT,
commonname VARCHAR(35) NOT NULL,
username VARCHAR(9) NOT NULL,
pswd VARCHAR(32) NOT NULL,
PRIMARY KEY(rowID));
My php code is as follows (with the exception of my username and password):
<?php
/* Because the authentication prompt needs to be invoked twice,
embed it within a function.
*/
function authenticate_user() {
header('WWW-Authenticate: Basic realm="Secret Stash"');
header("HTTP/1.0 401 Unauthorized");
exit;
}
/* If $_SERVER['PHP_AUTH_USER'] is blank, the user has not yet been prompted for
the authentication information.
*/
if (! isset($_SERVER['PHP_AUTH_USER'])) {
authenticate_user();
} else {
// Connect to the MySQL database by entering the MySQL server, username, and password. Also enter the db on next line.
$conn = mysql_connect("localhost", "myusername", "mypassword");
$db = mysql_select_db("authenticationtest");
// Create and execute the selection query.
$query = "SELECT username, pswd FROM userauth
WHERE username='$_SERVER[php_AUTH_USER]' AND
pswd=md5('$_SERVER[php_AUTH_PW]')";
$result = mysql_query($conn, $query);
// If nothing was found, reprompt the user for the login information.
if (mysql_num_rows($result) == 0) {
authenticate_user();
}
else {
echo "Welcome to the Authentication Test!";
}
}
?>