Content · Web Development · PHP

Verifying Login Passwords with password_verify()

Compare a submitted password with its stored adaptive password hash using.

1 views Published Oct 17, 2026 Updated Oct 17, 2026 admin

Compare a submitted password with its stored adaptive password hash using password_verify. The examples separate the essential operation from the conditions a production implementation must enforce.

What It Does


Compare a submitted password with its stored adaptive password hash using password_verify.

Prerequisites


Use a dedicated development environment with test accounts and records. Separate authorization, validation and error handling from response formatting; never copy demonstration-only secrets into production.

Example 1: Core Behavior


The first example isolates the core operation so its result is easy to understand.

<?php
$stored = password_hash('test-passphrase', PASSWORD_DEFAULT);
echo password_verify('test-passphrase', $stored) ? 'Logged in' : 'Denied';

Example 2: Alternative or Failure Case


The second example demonstrates an alternative outcome, rule or failure condition.

<?php
$stored = password_hash('valid-password', PASSWORD_DEFAULT);
echo password_verify('invalid-password', $stored) ? 'Accepted' : 'Rejected';

Practical Example: Applying the Rule


The practical example shows how to carry the same constraint into application code.

<?php
$stored = password_hash('demo', PASSWORD_DEFAULT);
if (password_verify('demo', $stored) && password_needs_rehash($stored, PASSWORD_DEFAULT)) { $stored = password_hash('demo', PASSWORD_DEFAULT); }
echo 'Authentication checked';

Important Behavior and Edge Cases


Always use the whole stored hash and generic login errors; apply rate limits and secure sessions.

Comparison with Related Functions or Patterns


ApproachWhen it differs
Verifying Login Passwords with password_verify()Compare a submitted password with its stored adaptive password hash using password_verify.
Related API or patternpassword_needs_rehash detects whether to update cost parameters after login.

Common Beginner Mistakes


MistakeCorrect approach
Comparing two freshly generated password hashes for equalityAlways use the whole stored hash and generic login errors; apply rate limits and secure sessions.
Substituting a related API without checking its semanticspassword_needs_rehash detects whether to update cost parameters after login.

Key Takeaways


  • Compare a submitted password with its stored adaptive password hash using password_verify.
  • Always use the whole stored hash and generic login errors; apply rate limits and secure sessions.
  • password_needs_rehash detects whether to update cost parameters after login.

Select this API or pattern based on its documented behavior and the trust boundaries in your own application, and test all external integrations against their actual deployments.

Share this resourceSend this page to someone who may find it useful.