Please help with Session Authentication

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
polosport6699
Forum Commoner
Posts: 35
Joined: Wed Jun 11, 2003 3:19 pm

Please help with Session Authentication

Post by polosport6699 »

Im attempting to write an authentication template using sessions but I keep getting an error with if (mysql_num_rows($result) >0 )

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in c:\inetpub\wwwroot\authmain.php on line 16

I cant figure out what my problem is. Anyone?? Thanks for the help

Code: Select all

<?php
session_start();

if (isset($HTTP_POST_VARS['userid']) && isset($HTTP_POST_VARS['password']))
{
  // if the user has just tried to log in
  $userid = $HTTP_POST_VARS['userid'];
  $password = $HTTP_POST_VARS['password'];

  $db_conn = mysql_connect('localhost', 'root', '');
  mysql_select_db('mydb', $db_conn);
  $query = 'select * from auth '
           ."where name='$userid' "
           ." and pass=password('$password')";
  $result = mysql_query($query, $db_conn);
  if (mysql_num_rows($result) >0 )
  {
    // if they are in the database register the user id
    $HTTP_SESSION_VARS['valid_user'] = $userid;    
  }
}
?>
<html>
<body>
<h1>Home page</h1>
<? 
  if (isset($HTTP_SESSION_VARS['valid_user']))
  {
    echo 'You are logged in as: '.$HTTP_SESSION_VARS['valid_user'].' <br />';
    echo '<a href="logout.php">Log out</a><br />';
  }
  else
  {
    if (isset($userid))
    {
      // if they've tried and failed to log in
      echo 'Could not log you in';
    }
    else 
    {
      // they have not tried to log in yet or have logged out
      echo 'You are not logged in.<br />';
    }

    // provide form to log in 
    echo '<form method="post" action="authmain.php">';
    echo '<table>';
    echo '<tr><td>Userid:</td>';
    echo '<td><input type="text" name="userid"></td></tr>';
    echo '<tr><td>Password:</td>';
    echo '<td><input type="password" name="password"></td></tr>';
    echo '<tr><td colspan="2" align="center">';
    echo '<input type="submit" value="Log in"></td></tr>';
    echo '</table></form>';
  }
?>
<br>
<a href="file:///C|/Documents%20and%20Settings/Jnarowski/Desktop/members_only.php">Members section</a>
</body>
</html>
User avatar
Wayne
Forum Contributor
Posts: 339
Joined: Wed Jun 05, 2002 10:59 am

Post by Wayne »

find out where the code is crashing, try replacing the mysql lines with:

Code: Select all

$db_conn = mysql_connect('localhost', 'root', '') or die('failed to connect'); 
mysql_select_db('mydb', $db_conn) or die('failed to select db'); 
$result = mysql_query($query, $db_conn) or die('failed to execute query');
polosport6699
Forum Commoner
Posts: 35
Joined: Wed Jun 11, 2003 3:19 pm

Post by polosport6699 »

That helps somewhat, Now it says failed to execute query. I have changed the vaulues around and still cant get it to work...?
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

Add in a call to mysql_error() to see what MySQL says about the error:

Code: Select all

$result = mysql_query($query, $db_conn) or die(mysql_error().'<p>'.$query.'</p>');
Mac
Post Reply