Rachel Dockter
Rachel Dockter

Reputation: 986

Cant verify users hashed password - mysql & php

im trying to verify the users hashed password with their input but i cant get it working, so far it idenfities if theres a user with that username but it just wont verify the password. here is my code

    <?php
    $serverName = "localhost"; //Variables to access the user database
    $username = "root";
    $password = "";
    $database = "snake_database";
    $errors = []; //Array of all the errors to display to the user

    $conn = mysqli_connect($serverName, $username, $password, $database); //Connect to the database

    if(!$conn){ //If the database failed to connect

        die("Database failed to connect: " .mysqli_connect_error()); //Display an error message
    }

    $username = $_POST['username']; //set the username/ password varaibles
    $password = $_POST['password'];
    $hashPass = password_hash($password, PASSWORD_DEFAULT); //Encrypt the password

    $sql = "SELECT * FROM users WHERE username = ?"; //Select all usernames and passwords
    $stmt = $conn->prepare($sql);
    $stmt->bind_param("s", $username);
    $stmt->execute();
    $result = $stmt->get_result();
    $count = mysqli_num_rows($result); //Count how many results there are

    if ($count == 1)
    {
         $sql = "SELECT password FROM users WHERE username = ?";
        $stmt = $conn->prepare($sql);
        $stmt->bind_param("s", $username);
        $stmt->execute();
        $result = $stmt->get_result();

        if(password_verify($password, $result )){
            $count = 2;
        }
    }

    if($count == 2) //If there is 1 account that matches
    {
        $stmt->close(); //Close the statment and connection
        $conn->close();

        session_start();
        $_SESSION["LoggedUser"] = $username; //Log the user in
        $_SESSION["lastPage"] = "login.php";
        header("location: profile.php"); //Direct the user to their profile

    }else //if there is no accounts that match
    {
        array_push($errors, "Username or password is incorrect");
        session_start();    
        $_SESSION["loginErrors"] = $errors;
        $_SESSION["lastPage"] = "login.php"; //Make this page the last page
        header("location: index.php"); //Go to the homepage
    }
    ?>

any help is appriciated, thanks

Upvotes: 0

Views: 220

Answers (1)

RiggsFolly
RiggsFolly

Reputation: 94682

You are doing a lot of things you dont need to do.

A SELECT * will return all the columns so you dont need to do another SELECT for just the password.

Also you should not password_hash() the password again, when checking a password against the one already stored on the database. Use password_verify() and that will do all the checking. So you pass it the hashed_password from the database and the plain text password the user just entered on the screen, it will return true or false telling you if the password entered matched the hashed one on the database

<?php
// always do this early in the code
session_start();

$serverName = "localhost";
$username = "root";
$password = "";
$database = "snake_database";
$errors = []; //Array of all the errors to display to the user

$conn = mysqli_connect($serverName, $username, $password, $database); 

if(!$conn){ 
    die("Database failed to connect: " .mysqli_connect_error()); 
}

// dont hash password again 
//$hashPass = password_hash($password, PASSWORD_DEFAULT); 

$sql = "SELECT * FROM users WHERE username = ?"; 
$stmt = $conn->prepare($sql);
$stmt->bind_param("s",  $_POST['username']);
$stmt->execute();

$result = $stmt->get_result();

if ($result->num_rows == 1) {

    $row = $result->fetch_assoc();
    if(password_verify($_POST['password'], $row['password'] )){
    // ----------------^^^^^^^^^^^^^^^^^^--^^^^^^^^^^^^^^^^
    //                  Plain text pwd      hashed pwd from db  
        $_SESSION["LoggedUser"] = $_POST['username']; 
        $_SESSION["lastPage"] = "login.php";
        header("location: profile.php"); 
        // put exit after a redirect as header() does not stop execution
        exit;
    }

} else {
    $errors[] = "Username or password is incorrect";
    $_SESSION["loginErrors"] = $errors;
    $_SESSION["lastPage"] = "login.php";
    header("location: index.php"); 
    exit;
}
?>

Upvotes: 2

Related Questions