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
https://forums.phpfreaks.com/topic/250056-login-check/
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
https://forums.phpfreaks.com/topic/250056-login-check/#findComment-1283285
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
https://forums.phpfreaks.com/topic/250056-login-check/#findComment-1283665
Share on other sites

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.