Password is not verified using function password_verify

前端 未结 4 1753
轮回少年
轮回少年 2020-12-12 05:36

I think i have hashed password using function PASSWORD directly from mysql database(am i doing wrong here?). And i am trying to verify that password with this c

4条回答
  •  余生分开走
    2020-12-12 05:44

    You must use password_hash to encode passwords verified with password_verify.

    The MySQL function PASSWORD is something entirely different. It is used for encoding passwords specific to MySQL authentication. (MySQL specifically recommends against using PASSWORD for anything other than MySQL authentication.)

    The two use different hashing algorithms, present their output in different formats, and are generally not compatible with each other.


    The typical way to use password_hash and password_verify is:

    $hash = password_hash($password, PASSWORD_DEFAULT);
    //Store $hash in your database as the user's password
    
    //To verify:
    //Retrieve $hash from the database, given a username
    $valid = password_validate($password, $hash);
    

    The problem in your code is that you're doing this:

    $password=password_verify($password,$hash);
    $sql = "select * from admin where username = '" . $first . "' and password = '". $password . "'";
    

    password_verify returns a boolean (whether the password and hash matched). Instead, you need to retrieve the hash from the database and match the entered password with that hash.

提交回复
热议问题