php password_hash and password_verify issues no match

php password_hash and password_verify issues no match

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

The $2y$ Prefix and the Hash String Order

When transitioning from md5() or sha1() to the modern password_hash() function introduced in PHP 5.5, the most common stumbling block isn't the algorithm itself; it's how you handle existing hashes generated by legacy code. A frequent scenario involves taking a hash string that looks like $2y$10$fXJEsC0zWAR2tDrmlJgSaecbKyiEOK9GDCRKDReYM8gH2bG2mbO4e and expecting it to verify against a plain text password.

If you are seeing "no match" errors in your password_verify() implementation, the culprit is almost certainly the order of arguments or the handling of salted hashes from older systems. In the standard API, the function signature is password_verify($password, $hash). The first argument must be the raw plaintext password, and the second must be the hash string generated by password_hash().

Consider a scenario where you are porting a legacy authentication system. You have extracted a hash string that begins with $2y$, which indicates it uses the crypt_blowfish algorithm (bcrypt). This specific prefix is crucial because it tells the verification function exactly how to parse the rest of the string. The string format generally follows salted_hash = [algorithm] + [cost] + [salt] + [hash].

In your specific case, if you are manually constructing the check or reusing a hash from a database without regenerating it, ensure you are not accidentally reversing the arguments or modifying the hash string itself. The code snippet below demonstrates the correct usage for a standard bcrypt hash:

$password = "test";
$hash = '$2y$10$fXJEsC0zWAR2tDrmlJgSaecbKyiEOK9GDCRKDReYM8gH2bG2mbO4e';

if (password_verify($password, $hash)) {
    echo "Success";
} else {
    echo "Error";
}

The $2y$ variant is designed to be self-describing. Unlike older crypt formats that require you to prepend the algorithm identifier manually (e.g., crypt($password, '$2a$...')), password_hash() automatically generates a string with the necessary prefix and cost factor attached. When verifying, you should never strip the $2y$ or $2a$ prefix. In fact, stripping it will cause verification to fail immediately because password_verify() relies on that prefix to determine the hashing algorithm and extract the salt embedded within the string.

A critical pitfall occurs when developers attempt to verify a password against a hash generated by a different version of PHP or a different library configuration. The cost factor (the number after $10$ in your example) dictates how many iterations the hashing process runs. If you manually generate a new hash with a higher cost factor than the original, verification will fail because the resulting hashes will not be identical. However, password_verify() is robust enough to accept a hash generated with a lower or equal cost factor compared to what password_hash() might currently default to.

If you are building a new system and do not have access to the original plaintext password used to generate an old legacy hash, you cannot "decrypt" it back. You can only verify if a candidate password matches. If your application logic requires storing hashes that are compatible with both old crypt() functions and new password_hash() standards, you might find yourself dealing with hashes that lack the $2y$ prefix. In those cases, you must explicitly check the algorithm before passing it to password_verify().

For high-volume applications or those requiring centralized security policies, managing these hashes manually can become error-prone. This is where frameworks like https://laravelcompany.com shine, as they abstract away the complexity of hashing algorithms and cost factor adjustments, ensuring that your stored credentials always use a secure, consistent standard without manual intervention.

Ultimately, if password_verify() returns false, check three things: the argument order, the integrity of the hash string (ensure no whitespace was trimmed or added), and whether you are comparing against a hash generated with an incompatible algorithm version.

Our network