How to verify an hashed password

169 views Asked by At

I am using the password_hash() function.

Now it works to hash the password, but how do I verify it?

2

There are 2 answers

0
AudioBubble On BEST ANSWER

Well the function for this option is called: password_verify.

How it does work is this;

<?php
$password = "[PASS]"; //Password user fill in.
$hash= "[HASH]"; //The hashed password that you saved.
$checkPass = password_verify($password, $hash); //This returns a boolean; true or false
if ($checkPass == true)
{
  echo 'Password is good!';
}
else
{
  echo 'Password is wrong!';
}
?>
0
user3106974 On
boolean password_verify ( string $password , string $hash )

Verifies that the given hash matches the given password.

Note that password_hash() returns the algorithm, cost and salt as part of the returned hash. Therefore, all information that's needed to verify the hash is included in it. This allows the verify function to verify the hash without needing separate storage for the salt or algorithm information.

password The user's password.

hash A hash created by password_hash()

http://php.net/manual/en/function.password-verify.php