If your login code still runs a password through md5() or sha1() and stores the hex string, stop. Those functions are fine for checksums. They are the wrong tool for secrets people type into a form.
Fast hashes are a gift to attackers. Password storage needs a slow, salted, adaptive algorithm. PHP already ships one.
Use password_hash and password_verify
From the password_hash manual, the modern shape is:
```php $hash = password_hash($password, PASSWORD_DEFAULT); ```
PASSWORD_DEFAULT picks PHP's current recommended algorithm (today that is bcrypt via PASSWORD_BCRYPT, and it can change in a later PHP release). That change is intentional. Store the full string password_hash returns โ algorithm, cost and salt are baked into it.
Checking a login is one call. See password_verify:
```php if (password_verify($password, $hash)) { // credentials match } ```
password_verify is timing-safe for the comparison it performs. Do not reinvent that with === on a homemade digest.
Example from the PHP manual pattern
```php $password = 'correct horse battery staple';
$hash = password_hash($password, PASSWORD_DEFAULT); if ($hash === false) { throw new RuntimeException('password_hash failed'); }
var_export(password_verify('correct horse battery staple', $hash)); // true var_export(password_verify('wrong password', $hash)); // false ```
Never log the plaintext password. Never put it in an error message. Never email it back to the user "for convenience".
Rehash when the default moves on
When you upgrade PHP, or raise the bcrypt cost, existing rows may need a stronger hash. That is what password_needs_rehash is for:
```php if (password_verify($password, $hash) && password_needs_rehash($hash, PASSWORD_DEFAULT) ) { $hash = password_hash($password, PASSWORD_DEFAULT); // save $hash back to the user row } ```
Do this on successful login. You only have the plaintext in memory at that moment โ which is exactly when you can upgrade the stored hash without a mass reset.
Habits that keep you out of trouble
- Prefer
PASSWORD_DEFAULTunless you have a measured reason to pinPASSWORD_BCRYPTor another supported algorithm. - Column width: give the hash column enough room (60+ characters historically for bcrypt; 255 is a safe modern default).
- Rate-limit login attempts at the application or edge. A perfect hash does not stop credential stuffing on its own.
- Migrate old
md5/sha1rows on next successful login: verify the legacy digest once, then replace it withpassword_hashand delete the legacy field.
Why this makes money sense
Broken auth is unpaid overtime. A clean password_hash / password_verify path is cheap insurance. If it makes money, it makes sense โ and keeping customer logins boring is part of that.
Deeper PHP habits live in PHP Done The Correct Way (paperback on Amazon UK, Kindle UK, free companion code on the PHP AIR code page). More practical notes on the PHP AIR blog, or start at phpair.co.uk.