Open-Source PHP Programming • Foundational • Estimated study time: 45 minutes.
A login service must be able to check a password without retaining the original secret. PHP’s password-hashing API makes repeated guesses expensive. This lesson builds on persistent data handling and session security.
Learning objectives
Distinguish hashing from reversible encryption; use password_hash, password_verify and password_needs_rehash; explain random salts and Argon2id memory costs; safely upgrade stored hashes; test correct and incorrect credentials.
Instructional tutorial on PHP password_hash and password_verify. Consult current security guidance for algorithm selection.
Password storage and verification
PHP generates a random salt and encodes the algorithm, parameters and salt within the returned hash. Store the full encoded hash, never the original secret. The PHP manual recommends a storage field allowing future changes, such as 255 characters. OWASP guidance recommends Argon2id with a baseline of 19 MiB memory, two iterations and parallelism of one. Benchmark the deployed environment.

Worked example: six verification checks
Save as osphp010.php. Use a PHP build with Argon2id. The example uses synthetic data and applies exception handling and input validation concepts. No real account or database is accessed.
<?php
declare(strict_types=1);
function check(bool $ok, string $name): void {
if (!$ok) throw new RuntimeException($name);
echo "PASS: $name\n";
}
$demo = 'local-demo-only-phrase';
$algo = PASSWORD_ARGON2ID;
$old = ['memory_cost'=>19456,'time_cost'=>2,'threads'=>1];
$new = ['memory_cost'=>19456,'time_cost'=>3,'threads'=>1];
$hash = password_hash($demo, $algo, $old);
check(password_verify($demo, $hash), 'correct password');
check(!password_verify('wrong-demo', $hash), 'wrong password');
check($hash !== password_hash($demo, $algo, $old), 'unique salts');
check(password_needs_rehash($hash, $algo, $new), 'upgrade needed');
if (password_verify($demo, $hash) && password_needs_rehash($hash, $algo, $new)) {
$hash = password_hash($demo, $algo, $new);
}
check(password_verify($demo, $hash), 'upgraded hash verifies');
check(!password_needs_rehash($hash, $algo, $new), 'policy current');
Run php -l osphp010.php and php osphp010.php. PHP 8.4.24 reported no syntax errors and returned all six results:
PASS: correct password
PASS: wrong password
PASS: unique salts
PASS: upgrade needed
PASS: upgraded hash verifies
PASS: policy current
Memory costs and safe upgrades
The memory-cost setting uses KiB: m = 19,456 KiB = 19 MiB. For N concurrent hashing operations, nominal algorithm memory M = N × m. With N = 32, M = 608 MiB, excluding PHP processes and other memory overhead. This is a planning estimate, not a benchmark. Apply rate limits, protect logs, benchmark under load, use HTTPS and secure sessions, and consider MFA. Rehash only after successful authentication. Update the stored hash atomically to avoid overwriting concurrent credential changes.
This community troubleshooting thread illustrates why hash storage length matters. Never print real passwords or hashes into logs.
Exercises and answer key
A. Is password hashing reversible encryption? B. Why do hashes of the same password differ? C. What does password_verify return for a wrong password? D. When should a hash be upgraded? E. Calculate nominal algorithm memory for eight concurrent operations at 19 MiB each.
Answers: A. No. B. Random salts. C. false. D. After successful verification when policy requires rehashing. E. 152 MiB. Further reading: PHP password_needs_rehash. Featured artwork is conceptual, not a screenshot of the tested code.
Editor’s Note: We volunteer daily to ensure the credibility of the information on this platform is Verifiably True. If you would like to support to help further secure the integrity of our research initiatives, please scan the QR code below

For mobile scanning, open the article on another display and scan the QR code with a Bitcoin wallet. The voluntary donation amount is 5,000 sats. Verify the receiving address 3C9o19EH5HSiwEPyCTmEKzxhNCbo2X6TTb before sending.
BitcoinVersus.Tech publishes technology and financial journalism for informational purposes and does not provide financial, investment, or legal advice.

Leave a Reply