PHP password_verify(): Check a password against its stored hash

password_verify($password, $hash) returns true when the submitted password matches the stored hash, and false otherwise. The first argument is the submitted string. The second is the complete hash for that account. The function does not recover the original password. PHP Manual · password_verify()

Read the two arguments and the return value

The stored string includes the algorithm, work factor, and salt. Read the hash for the right account and use a PHP runtime that supports its algorithm. There are no separate algorithm or salt arguments. See the password_hash() generation and storage examples for generation and storage.

Try correct input and a truncated hash

With PHP 8.x, save the code as UTF-8 in password-verify-demo.php and run the command in that folder. The public sample tests an exact match, an unrelated input, and an input with an added space.

<?php
declare(strict_types=1);
$registeredPassword = 'sample-only passphrase 42';
$storedHash = password_hash($registeredPassword, PASSWORD_DEFAULT);
$cases = [
    'Correct input' => $registeredPassword,
    'Different input' => 'different input',
    'Added space' => ' ' . $registeredPassword,
];
foreach ($cases as $label => $submittedPassword) {
    echo $label, ': ', password_verify($submittedPassword, $storedHash) ? 'passed' : 'failed', PHP_EOL;
}
$newHash = password_hash($registeredPassword, PASSWORD_DEFAULT);
echo 'Direct comparison with a new hash: ', $storedHash === $newHash ? 'same' : 'different', PHP_EOL;
echo 'Truncated hash: ', password_verify($registeredPassword, substr($storedHash, 0, -8)) ? 'passed' : 'failed', PHP_EOL;
echo 'Unsupported format: ', password_verify($registeredPassword, 'not-a-password-hash') ? 'passed' : 'failed', PHP_EOL;
php password-verify-demo.php
Correct input: passed
Different input: failed
Added space: failed
Direct comparison with a new hash: different
Truncated hash: failed
Unsupported format: failed

Only the exact input passes. A new hash of the same password normally differs because the salt is random. Pass the submitted string and the stored value to the verification function. Comparing a freshly generated hash with the saved one does not verify the password. The truncated and unsupported strings also fail in the example.

Diagnose consistently failed checks

Check the following in order. Even in restricted diagnostics, record only what you need, such as lookup results, string length, and format. Keep passwords and full hashes out of logs. PHP Manual · Password Hashing

  1. Check that the correct account supplied the complete hash and that the database did not truncate it on write or read.
  2. Check whether registration and login handle spaces, encoding, or input normalization differently.
  3. Check that PHP supports the stored algorithm. A plain MD5 digest or ciphertext is not converted automatically.
  4. Distinguish absent values and arrays in web submissions. the PHP GET and POST validation examples

bcrypt processes the first 72 bytes. Set an algorithm and a byte-length policy for new and changed passwords. Before applying a new limit to existing accounts, review how their hashes were produced and how you will migrate them. PHP Manual · password_hash()

Raise the bcrypt cost after verification

Save the next example as password-rehash-demo.php and run php password-rehash-demo.php. The bcrypt costs of 10 and 12 illustrate an upgrade. password_needs_rehash() compares a hash with the target policy. Check the password before you call it. PHP Manual · password_needs_rehash()

<?php
declare(strict_types=1);
function verifyAndUpgrade(array &$record, string $submitted): bool {
    if (!password_verify($submitted, $record['password_hash'])) {
        return false;
    }
    $targetAlgorithm = PASSWORD_BCRYPT;
    $targetOptions = ['cost' => 12];
    $info = password_get_info($record['password_hash']);
    if ($info['algoName'] === 'bcrypt'
        && $info['options']['cost'] < $targetOptions['cost']
        && password_needs_rehash($record['password_hash'], $targetAlgorithm, $targetOptions)) {
        $record['password_hash'] = password_hash($submitted, $targetAlgorithm, $targetOptions);
    }
    return true;
}
$password = 'sample-only passphrase 42';
$record = ['id' => 7, 'password_hash' => password_hash($password, PASSWORD_BCRYPT, ['cost' => 10])];
$oldHash = $record['password_hash'];
echo 'Wrong input: ', verifyAndUpgrade($record, 'different input') ? 'passed' : 'failed', PHP_EOL;
echo 'Hash unchanged after failure: ', $record['password_hash'] === $oldHash ? 'yes' : 'no', PHP_EOL;
echo 'Correct input: ', verifyAndUpgrade($record, $password) ? 'passed' : 'failed', PHP_EOL;
echo 'Verify the new hash: ', password_verify($password, $record['password_hash']) ? 'passed' : 'failed', PHP_EOL;
echo 'New cost: ', password_get_info($record['password_hash'])['options']['cost'], PHP_EOL;
$upgradedHash = $record['password_hash'];
verifyAndUpgrade($record, $password);
echo 'Hash unchanged at target cost: ', $record['password_hash'] === $upgradedHash ? 'yes' : 'no', PHP_EOL;
Wrong input: failed
Hash unchanged after failure: yes
Correct input: passed
Verify the new hash: passed
New cost: 12
Hash unchanged at target cost: yes

A wrong input leaves the hash alone. A correct input raises a lower bcrypt cost to 12, and the replacement still verifies the same password. The example leaves costs of 12 or above and hashes from other algorithms alone. Define algorithm migrations separately.

In a database, use the account ID and the previously read hash or version in the update condition so a concurrent password reset is preserved. Bind parameters, compare hash bytes exactly, and confirm the write succeeded.

Handle accounts, authorization, and sessions

true confirms a password match. Check account status, access rights, and any additional authentication before issuing a session. Timing protection within this function does not cover the whole login flow. Use a consistent login failure response, HTTPS, and attempt limits. OWASP · Authentication Cheat Sheet