Orofold Labs

Launching soon

Notes Get in touch

Engineering 4 min read Updated

Storing passwords correctly in PHP

In short

Store passwords with password_hash() using PASSWORD_DEFAULT or Argon2id, check them with password_verify(), and rehash older hashes at the next login.

A password table is only as safe as the way it stores passwords. If an attacker copies the table, the stored values should cost them real time and money before they can recover the passwords. Store passwords with PHP’s built-in functions, choose a strong algorithm, handle the 72-byte limit of bcrypt and upgrade old hashes at login. None of this requires cryptography of your own.

Hash the password, do not encrypt it

The application never needs the original password back. It only needs to check whether the password typed at login is the right one. A one-way hash fits that job: the application computes the hash of what the visitor typed and compares it with the stored value. Encryption is reversible by design, so anyone who obtains the key also obtains every password. A good password hash is deliberately slow, which makes guessing expensive.

Use password_hash() and password_verify()

PHP provides password_hash() to create a hash and password_verify() to check one. Pass PASSWORD_DEFAULT to let PHP choose the algorithm. The manual says that PASSWORD_DEFAULT currently means bcrypt, and that the constant is designed to change over time, so the length of the output can change as well. Store the hash in a column that holds more than 60 bytes; the manual suggests 255.

If your PHP build supports it, PASSWORD_ARGON2ID selects Argon2id. The manual says this option is available only when PHP was compiled with Argon2 support, and that it was added in PHP 7.3.0. Check your hosting before you rely on it.

Choose an algorithm in order of preference

OWASP ranks the algorithms in the table below. The values are the minimums that its cheat sheet gave when we checked it on 24 November 2026.

AlgorithmOWASP guidance
Argon2idPreferred. At least 19 MiB of memory, two iterations and one degree of parallelism.
scryptWhen Argon2id is not available. A cost parameter of at least 2^17, block size 8 and parallelisation 1.
bcryptOnly for legacy systems where neither Argon2 nor scrypt is available. A work factor of 10 or more.
PBKDF2When FIPS-140 compliance is required. A work factor of at least 600,000, with HMAC-SHA-256.

The manual gives 12 as the default bcrypt cost from PHP 8.4.0, which is above the OWASP minimum. A higher cost also makes each login slower, so measure the value on your own hardware before you set it.

Working on something similar in your own product? A few honest sentences are enough – we read every enquiry and reply by email. Apply with your idea

Mind the 72-byte limit of bcrypt

The manual says that PASSWORD_BCRYPT truncates the password to 72 bytes. OWASP says most bcrypt implementations have the same limit, and advises a maximum password length of 72 bytes, or less where your implementation has a smaller limit. PHP counts bytes rather than characters, so a password with accented letters uses more of the limit than its visible length suggests. Enforce the maximum in the registration and password-change forms, so visitors learn about it before they submit.

Upgrade old hashes when users next log in

Recommended work factors rise as hardware improves. OWASP describes the usual approach: wait until the user next authenticates, then re-hash the password with the new work factor. Accounts that never log in again keep their older hash, and that is expected. To decide whether a stored hash needs replacing, call password_needs_rehash(). The manual describes it as checking whether a hash matches the given options.

Some advice here is general engineering practice, such as not writing your own hashing scheme and not logging passwords. OWASP mentions a pepper as an optional extra layer. A pepper adds no protection on its own and never replaces a slow algorithm.

Sources and limits

A practical checklist

  • Create hashes with password_hash() and PASSWORD_DEFAULT, or with PASSWORD_ARGON2ID where your PHP build supports it.
  • Check passwords with password_verify(), never by comparing hash strings yourself.
  • Store hashes in a column that holds at least 255 bytes.
  • Enforce a maximum password length of 72 bytes.
  • After each successful login, call password_needs_rehash() and store the new hash when it returns true.
  • Keep passwords and hashes out of logs, error messages and support tickets.

A strong hash is only one layer. What happens after a successful login is covered in sessions and cookies done safely. Limiting repeated guesses is covered in Rate limiting that protects forms and logins, and a second factor for privileged accounts is described in two-factor authentication for admin access.

Common questions

Can passwords be stored in a form that can be decrypted later?
No. The application only has to check a typed password, and a one-way hash is the right tool for that check.
Which algorithm should I choose?
OWASP prefers Argon2id and suggests scrypt where Argon2id is unavailable, with bcrypt only for legacy systems. According to the PHP manual, PASSWORD_DEFAULT currently selects bcrypt.
What happens to hashes made with an older setting?
OWASP suggests re-hashing the password with the new work factor when the user next authenticates. password_needs_rehash() shows whether a stored hash matches the current options.

Sources

  1. PHP: password_hash - Manual, PHP Group (php.net). Accessed 24 November 2026
  2. Password Storage - OWASP Cheat Sheet Series, OWASP Foundation. Accessed 24 November 2026

Further reading

Have an idea that should exist? Or a working product that needs more customers, a new website or a complete rework? Bring us anything. Apply with your idea

All notes