Verify Password Hashes with password_verify()

Verify the submitted password against the stored hash

Fetch the stored hash for the account, then use password_verify(). Do not hash the submitted password again and compare strings.

<?php
$submittedPassword = $_POST['password'] ?? '';
$storedHash = $user['password_hash'] ?? '';

if ($storedHash !== '' && password_verify($submittedPassword, $storedHash)) {
    echo 'Password accepted';
} else {
    echo 'Invalid password';
}
?>

Current example first; the detailed tutorial examples below are retained for additional learning and comparison.

$str="plus2net.com";
$str_hash=password_hash($str,PASSWORD_DEFAULT);
//echo $str_hash;

if(password_verify($str,$str_hash)){
echo " Matching  ";
}else{
echo " Not matching ";
}	
Output is
Matching
Syntax
password_verify('$entered_string',$hash_string);
ParameterDESCRIPTION
$entered_stringRequired : Input password string ( user entered )
$hash_stringRequired : Hash created by password_hash().
Output is Boolean , True or False

Always include the has variable in single quotes and not in double quotes. The presence of $ will change the value

crypt() and password_ferify()

$str="plus2net.com";
$str_crypt_hash = crypt($str);
///////////
if(password_verify('plus2net.com',$str_crypt_hash)){
echo " Matching  ";
}else{
echo " Not matching ";
}
Output is
Matching

Example: Verifying a Correct Password

$password = "secure123";
$hash = password_hash($password, PASSWORD_DEFAULT);
if (password_verify("secure123", $hash)) {
    echo "Password is correct!";
} else {
    echo "Password is incorrect!";
}

Example: Verifying an Incorrect Password

$password = "secure123";
$hash = password_hash($password, PASSWORD_DEFAULT);
if (password_verify("wrong_password", $hash)) {
    echo "Password is correct!";
} else {
    echo "Password is incorrect!";
}

Example: Checking if the Hash Needs Rehashing

$hash = password_hash("mypassword", PASSWORD_DEFAULT);
if (password_verify("mypassword", $hash)) {
    if (password_needs_rehash($hash, PASSWORD_DEFAULT)) {
        $newHash = password_hash("mypassword", PASSWORD_DEFAULT);
        echo "Password rehashed!";
    }
}

String Functions password_hash(): Generate password hash


Subscribe to our YouTube Channel here



plus2net.com











PHP video Tutorials
✖
We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
© 2000-2026 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer