PHP password_verify() not working with database

Kevin Shen

I'm in the process of making a login and registration system. The system works so now I have to add in security for hashing password for database storage. However, when I retrieve the hashed password from the database and comparing it to the one the user entered as the password input it doesn't work.

    <?php
session_start(); //start the session for user profile page

define('DB_HOST','localhost'); 
define('DB_NAME','test'); //name of database
define('DB_USER','root'); //mysql user
define('DB_PASSWORD',''); //mysql password

$con = new mysqli(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME) or die(mysqli_connect_error());

$db = mysqli_select_db($con,DB_NAME) or die(mysqli_connect_error()); 

/*
$ID = $_POST['user'];
$Password = $_POST['pass'];
*/
function SignIn(mysqli $con){
    $user = mysqli_real_escape_string($con,$_POST['user']); //user input field from html
    $pass = mysqli_real_escape_string($con,$_POST['pass']); //pass input field from html
    //$user = $_POST['user'];
    //$pass = $_POST['pass'];
    if(isset($_POST['user'])){ //checking the 'user' name which is from Sign-in.html, is it empty or have some text
        $query = mysqli_query($con,"SELECT * FROM UserName where userName = '$_POST[user]' AND pass = '$_POST[pass]'") or die(mysqli_connect_error());
        $row = mysqli_fetch_array($query); //or die(mysqli_error($con));
        $username = $row['userName'];
        $pw = $row['pass'];//hashed password in database
        //check username and password hash
        echo $pw; //THIS PRINTS OUT NOTHING!!!
        if($user==$username && password_verify($pass, $pw)) {
            // $user and $pass are from POST
            // $username and $pw are from the rows

            //$_SESSION['userName'] = $row['pass'];
            echo "Successfully logged in.";
        }

        else { 
            echo "Invalid."; 
        }
    }
    else{
        echo "INVALID LOGIN";
    }
}

if(isset($_POST['submit'])){
    SignIn($con);
}
?>

So the above code will echo "Invalid" when I attempt to compare the text password entered and the hashed one in the database. The echo $pw prints out nothing for some unknown reason.

Here is the Registration php script:

<?php
        //Connection Config
        define('DB_HOST','localhost'); 
        define('DB_NAME','test'); //name of database
        define('DB_USER','root'); //mysql user
        define('DB_PASSWORD',''); //mysql password
        $con = new mysqli(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME) or die(mysqli_connect_error());
        $db = mysqli_select_db($con,DB_NAME) or die(mysqli_connect_error()); 
        //Registration
        function Register($con){
            if(isset($_POST['user']) && isset($_POST['pass'])){
                $username = $_POST['user'];
                $email = $_POST['email'];
                $password = $_POST['pass'];

                //Hashing of password
                $hpassword = password_hash($password, PASSWORD_DEFAULT);
                $query = mysqli_query($con,"INSERT INTO UserName (UserNameID,userName, pass, email) VALUES ('2','$username','$hpassword','$email') ") or die(mysqli_connect_error());

                if($query){
                    //Query successful
                    echo "User has been created successfully";
                }else{
                    echo "Error1";
                }
            }else{
                echo "Error2";
            }
        }

        if(isset($_POST['submit'])){
            Register($con);
        }
    ?>

I've made sure the column is varchar(255) and long enough. Does anyone know why the verification fails? Thanks!

Note: After password hashing I'm planning to add SQL injection defenses.

wimg

You're inserting a hashed password, that's good. But then on login you're comparing the one on the POST string with the hashed version in the database. Logically, they will not be the same. You should change :

SELECT * FROM UserName where userName = '$_POST[user]' AND pass = '$_POST[pass]'"

into

SELECT * FROM UserName where userName = '$_POST[user]'

And indeed you should add protection against SQL injection everywhere. Preferably use prepared statements, on every select, insert, update, delete, etc. and on every single value you're using in those statements.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

password_verify in PHP not working using database

From Dev

password_verify() not working with database

From Dev

PHP password_hash and password_verify Not Working with MySQL

From Dev

password_verify not working on Raspberry Pi (PHP7)

From Dev

PHP password_verify()

From Dev

Password_verify bcrypt not working

From Dev

MySql Password_verify() not working?

From Dev

PHP - password_verify issue

From Dev

Using password_verify in PHP

From Dev

PHP login not working. Using password_verify and it seems to be causing some problems

From Dev

PHP login not working. Using password_verify and it seems to be causing some problems

From Dev

PHP password_hash(), password_verify()

From Dev

Password_verify is not verifying the hash in the database

From Dev

PHP password_verify() and slow equals comparison

From Dev

How to use password_verify function in PHP

From Dev

PHP password_verify always returns false

From Dev

PHP password_verify returning false

From Dev

How to use password_verify function in PHP

From Dev

PHP: password_verify always returns false

From Dev

PHP password_verify not functioning with prepared statement

From Dev

How to retrieve password from database with password_verify()?

From Dev

How to retrieve password from database with password_verify()?

From Dev

php password_hash and password_verify issues no match

From Dev

Are PHP's password_hash and password_verify functions enough?

From Dev

php password_hash and password_verify issues no match

From Dev

PHP - password_verify always returns false (incorrect password)

From Dev

password_verify return false from database but true from site?

From Dev

Another password_verify() issue possibly database related

From Dev

Is PHP password_verify really secure? Add another salt?

Related Related

  1. 1

    password_verify in PHP not working using database

  2. 2

    password_verify() not working with database

  3. 3

    PHP password_hash and password_verify Not Working with MySQL

  4. 4

    password_verify not working on Raspberry Pi (PHP7)

  5. 5

    PHP password_verify()

  6. 6

    Password_verify bcrypt not working

  7. 7

    MySql Password_verify() not working?

  8. 8

    PHP - password_verify issue

  9. 9

    Using password_verify in PHP

  10. 10

    PHP login not working. Using password_verify and it seems to be causing some problems

  11. 11

    PHP login not working. Using password_verify and it seems to be causing some problems

  12. 12

    PHP password_hash(), password_verify()

  13. 13

    Password_verify is not verifying the hash in the database

  14. 14

    PHP password_verify() and slow equals comparison

  15. 15

    How to use password_verify function in PHP

  16. 16

    PHP password_verify always returns false

  17. 17

    PHP password_verify returning false

  18. 18

    How to use password_verify function in PHP

  19. 19

    PHP: password_verify always returns false

  20. 20

    PHP password_verify not functioning with prepared statement

  21. 21

    How to retrieve password from database with password_verify()?

  22. 22

    How to retrieve password from database with password_verify()?

  23. 23

    php password_hash and password_verify issues no match

  24. 24

    Are PHP's password_hash and password_verify functions enough?

  25. 25

    php password_hash and password_verify issues no match

  26. 26

    PHP - password_verify always returns false (incorrect password)

  27. 27

    password_verify return false from database but true from site?

  28. 28

    Another password_verify() issue possibly database related

  29. 29

    Is PHP password_verify really secure? Add another salt?

HotTag

Archive