PHP password_hash(): Create, verify, and update password hashes

In PHP, use password_hash() to create a one-way password hash and store the entire returned string. At login, use password_verify() to check the input against the stored hash. The automatic salt normally makes the hashes different even for the same password. PHP Manual · password_hash() PHP Manual · password_verify()

Understand password hashes and salts

A salt is a random value used in hashing. It varies the result for identical inputs and is embedded in the hash along with algorithm and work-factor information. A separate salt column or manual salt is unnecessary. If someone forgets a password, verify their identity and reset it. Use password-specific hashing in place of md5() or plain SHA-256 for storage. PHP Manual · Password Hashing

Create and verify two hashes for one input

With PHP 8.x, save the code as UTF-8 in password-hash-demo.php and run the command from that folder. Use the public sample string only. The two hashes should differ, both verifications should succeed, and the different input should fail.

<?php
declare(strict_types=1);
function createPasswordHash(string $password): string {
    if ($password === '' || str_contains($password, "\0")) {
        throw new InvalidArgumentException('Empty passwords and NUL bytes are not allowed.');
    }
    $algorithm = PASSWORD_DEFAULT;
    if ($algorithm === PASSWORD_BCRYPT && strlen($password) > 72) {
        throw new InvalidArgumentException('A bcrypt password must be at most 72 bytes.');
    }
    return password_hash($password, $algorithm);
}

$password = 'sample-only passphrase 42';
$hash1 = createPasswordHash($password);
$hash2 = createPasswordHash($password);
echo 'Same hash: ', $hash1 === $hash2 ? 'yes' : 'no', PHP_EOL;
echo 'Verification 1: ', password_verify($password, $hash1) ? 'passed' : 'failed', PHP_EOL;
echo 'Verification 2: ', password_verify($password, $hash2) ? 'passed' : 'failed', PHP_EOL;
echo 'Different input: ', password_verify('different input', $hash1) ? 'passed' : 'failed', PHP_EOL;
echo 'Algorithm: ', password_get_info($hash1)['algoName'], PHP_EOL;
echo 'Length: ', strlen($hash1), PHP_EOL;
php password-hash-demo.php

A comparison between a newly generated hash and a stored hash cannot determine whether the passwords match. password_get_info() reports the algorithm and options for inspection. Keep passwords and hashes out of application logs. PHP Manual · password_get_info()

Choose an algorithm and work factor

Algorithm When to use it
PASSWORD_DEFAULT Use the current PHP default and allow the format or length to change.
PASSWORD_BCRYPT For existing compatibility needs, account for cost and the 72-byte limit.
PASSWORD_ARGON2ID Use a supporting build and review memory and time settings.
PASSWORD_ARGON2I A separate Argon2 variant available on builds that support it.

PASSWORD_DEFAULT currently selects bcrypt according to the PHP manual and may change later. For new systems, consider Argon2id, which OWASP prioritizes, and check password_algos() on the server. Save the second example as argon2-demo.php and run php argon2-demo.php. A supported build shows argon2id and a successful verification. Other builds show an availability message. PHP Manual · password_algos() OWASP · Password Storage Cheat Sheet

<?php
declare(strict_types=1);
echo 'Available algorithms: ', implode(', ', password_algos()), PHP_EOL;
if (!defined('PASSWORD_ARGON2ID') || !in_array('argon2id', password_algos(), true)) {
    echo 'This PHP build does not offer Argon2id.', PHP_EOL;
    exit;
}
$password = 'sample-only passphrase 42';
$hash = password_hash($password, PASSWORD_ARGON2ID);
echo 'Algorithm: ', password_get_info($hash)['algoName'], PHP_EOL;
echo 'Verification: ', password_verify($password, $hash) ? 'passed' : 'failed', PHP_EOL;

The bcrypt cost controls work. Its default increased from 10 to 12 in PHP 8.4. Argon2id includes memory and time settings. Check which options your implementation supports and measure latency and memory under realistic concurrent logins. The demo costs illustrate a transition and do not establish a production policy. PHP Manual · PHP 8.4 Changes

Handle bcrypt’s 72-byte input limit

bcrypt processes only the first 72 bytes. For new hashes, reject longer inputs before hashing or select a policy with an algorithm that supports them. The UTF-8 character 가 takes three bytes, so 24 copies take 72 bytes and 25 take 75. Spaces can be part of a password. Preserve them and avoid HTML encoding the input before hashing. This example rejects empty strings and NUL bytes.

Store hashes and rehash after login

Allow space for around 255 bytes as the PHP manual suggests, and verify the database returns the complete original hash. Use bound parameters to store it. If hashing fails, stop the request without substituting a plaintext password or an empty value.

Rehash only after the old hash successfully verifies the login. Pass the intended algorithm and options to password_needs_rehash(). If it returns true, create a new hash from the submitted password. Do not hash the old hash. Save password-rehash-demo.php and run php password-rehash-demo.php. It demonstrates a cost change using local variables and shows that a rehash is needed before the update and no longer needed afterward. PHP Manual · password_needs_rehash()

<?php
declare(strict_types=1);
$input = 'sample-only passphrase 42';
$stored = password_hash($input, PASSWORD_BCRYPT, ['cost' => 10]);
$options = ['cost' => 12];

if (!password_verify($input, $stored)) {
    echo 'Verification failed', PHP_EOL;
    exit(1);
}

$needed = password_needs_rehash($stored, PASSWORD_BCRYPT, $options);
echo 'Rehash needed before update: ', $needed ? 'yes' : 'no', PHP_EOL;
if ($needed) {
    $stored = password_hash($input, PASSWORD_BCRYPT, $options);
}

echo 'Rehash needed after update: ', password_needs_rehash($stored, PASSWORD_BCRYPT, $options)
    ? 'yes' : 'no', PHP_EOL;

In a database, replace the hash only if the account ID and the previously verified hash still match. This avoids overwriting a password reset that happened at the same time. A real login also needs HTTPS, attempt limits, and session management. Compare hash bytes exactly, including letter case.