Jump to content

sessions login script problem


will35010

Recommended Posts

I cannot get this script to work properly. It logs the user in and redirects them even if the credentials are invalid. How do I fix it please? Thanks!

 

<?php
session_start();

include('db.php');


if (isset($_POST['username'])) {
   
$username = $_POST['username'];
$password = $_POST['password'];    

//$username = mysqli_real_escape_string($_POST['username']);
//$password = mysqli_real_escape_string($_POST['password']);

// MySQL Query
$result = mysqli_query($conn, "SELECT * FROM staff WHERE username = '$username' AND password = '$password' ");

if(!$result) {
$_SESSION['error'] = '<span style="color: red">Login Failed</span>';
} else {
// Mysql fetch row results
$row = mysqli_fetch_assoc($result);

$_SESSION['userid'] = $row['id'];
$_SESSION['username'] = $username;
$_SESSION['error'] = 'Login successful<br> Welcome, '.$username;
header('Location: ./template.php');
}
}


?>

Link to comment
Share on other sites

$result will only be false if the query fails due to an error (sql syntax, database problem...) If the query executes without error, even if there are zero matching rows in the result set, $result will contain a result resource and pass the tests in your code.

 

You need to test the number of rows returned by the query.

Link to comment
Share on other sites

session_start();

include ('db.php');


if (isset($_POST['username'])) {

    $username = $_POST['username'];
    $password = $_POST['password'];

    //$username = mysqli_real_escape_string($_POST['username']);
    //$password = mysqli_real_escape_string($_POST['password']);

    // MySQL Query
    $result = mysqli_query($conn, "SELECT * FROM staff WHERE username = '$username' AND password = '$password' LIMIT 1 ");

    if (!$result || mysqli_num_rows($result) !== 1) {
        $_SESSION['error'] = '<span style="color: red">Login Failed</span>';
    } else {
        // Mysql fetch row results
        $row = mysqli_fetch_assoc($result);

        $_SESSION['userid'] = $row['id'];
        $_SESSION['username'] = $username;
        $_SESSION['error'] = 'Login successful<br> Welcome, ' . $username;
        header('Location: ./template.php');
        exit();
    }
}

Try that

Link to comment
Share on other sites

Thank you! I fixed it with this:

 

<?php
session_start();

include('db.php');


if (isset($_POST['username'])) {
   
$username = $_POST['username'];
$password = $_POST['password'];    

//$username = mysqli_real_escape_string($_POST['username']);
//$password = mysqli_real_escape_string($_POST['password']);

// MySQL Query
$result = mysqli_query($conn, "SELECT * FROM staff WHERE username = '$username' AND password = '$password' ");

// Mysql_num_row is counting table row
$count=mysqli_num_rows($result);

if($count==1){
    // Mysql fetch row results
$row = mysqli_fetch_assoc($result);

$_SESSION['userid'] = $row['id'];
$_SESSION['username'] = $username;
$_SESSION['error'] = 'Login successful<br> Welcome, '.$username;
header('Location: ./template.php');
}
else {
    echo "Login Failed";
}
}
?>

 

$result will only be false if the query fails due to an error (sql syntax, database problem...) If the query executes without error, even if there are zero matching rows in the result set, $result will contain a result resource and pass the tests in your code.

 

You need to test the number of rows returned by the query.

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.