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

      }

  }

?>

 

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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?

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.