Jump to content

Authenticating a User Against a MySQL Table


jrp91384

Recommended Posts

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!";

      }

  }

?>

 

Your mysql connection is failing for whatever reason...use error checking:

 

change

  $result = mysql_query($conn, $query);

to

  $result = mysql_query($conn, $query) or die(mysql_error());

and it will print out the error.

 

Do the same for your mysql_query call.

Your mysql connection is failing for whatever reason...use error checking:

 

change

  $result = mysql_query($conn, $query);

to

  $result = mysql_query($conn, $query) or die(mysql_error());

and it will print out the error.

 

Do the same for your mysql_query call.

 

It gives me error:

Warning: mysql_query(): supplied argument is not a valid MySQL-Link resource in C:\xampp\htdocs\test\passwordtest2.php on line 27

 

Sorry, but I'm not sure how to do the same for my mysql_query call?

 

What exactly does that warning mean?

mysql_query takes $string, $resource and you have it backwards, which is why I posted above the correct way.

 

I tried: $result = mysql_query($query) or die (mysql_error()); 

but when logging in it will not accept the username and password and goes directly to a blank page on the third attempt.

Any suggestions?

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.