Jump to content

login check


naveendk.55

Recommended Posts

Hi, I am trying to perform a login check and redirect the form to index.php if the username and password are correct. Also the login page check if the fields are entered or not.

 

The issue is that the login check is performing but not redirecting to index.php. Also I'm not getting any error after performing the login check.  Let me know if I'm making any errors in below code.  the include file only contains the database details.

 


<?php include("connections.php"); ?>
<?php
if(isset($_POST['submit']))
{
$errors=array();
$required_fields=array('username', 'pass');
foreach($required_fields as $fields)
{
    if(!isset($_POST[$fields]) || empty($_POST[$fields]))
    {
        $errors[]=$fields;
    }
}

if(empty($errors))
{
   $username=trim($_POST['username']);
   $password =trim($_POST['pass']);
    $query= "SELECT * FROM users WHERE username='{$username}' AND hashed_password='{$password}' ";
    $result=mysql_query($query);
    $result_set=mysql_fetch_array($result);
        if(mysql_num_rows($result_set) == 1)
    {
   header('location: index.php');
   exit;
    }
    else
    {
    echo 'Invalid password';
    }
}
   
} 
?>

 

 

Link to comment
Share on other sites

your error lies within theses lines..

 

$result=mysql_query($query);
    $result_set=mysql_fetch_array($result);
        if(mysql_num_rows($result_set) == 1)

 

you are attempting to use the mysql_num_rows() on a mysql_fetch_array() function, this is invalid... change the lines to this,

 

$result=mysql_query($query);
    $result_set=mysql_fetch_array($result);
        if(mysql_num_rows($result) == 1)

Link to comment
Share on other sites

Judging by the fact that your password field in the table is named hashed_password, I'm going to assume it actually contains a hashed password. You aren't hashing the plaintext password to compare against it, so if it is indeed hashed in the table, it will never match.

 

Also, it's inefficient to to run a SELECT * query when all you need to know is the number of records that match. A COUNT() query would be better here. Since you expect exactly one record to match, check for that condition as well as conditions that are not the expected result, and handle them accordingly.

$query = "SELECT COUNT(1) FROM users WHERE username='{$username}' AND hashed_password='{$password}' ";
$result = mysql_query($query);
$array = mysql_fetch_row($result);
if( $array[0] == 1 ) {
   header('location: index.php');
   exit;
} elseif( $array[0] == 0 ) {
        // There was no matching record, so handle that condition.
} else {
        // There was more than 1 matching record, indicating data corruption. This should be a fatal error condition, and should stop further processing.
}

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.