PHP Password

Beginner
8 min read
Updated: Nov 2026
3 examples
Hashing & logins

Definition and Usage

PHP has a small set of functions made for passwords. You never store the real password. You store a hash, which is a one-way scrambled version that cannot be turned back. password_hash() creates it, and password_verify() checks it at login.

A good password hash is slow on purpose and uses a random salt. PHP does both for you. The hash string even stores the algorithm, the cost and the salt, so a later check needs only the password and the hash.

This page explains the ideas. The Password Functions index lists all five function guides.

Pick the right tool.

Use password_hash() and password_verify() for passwords. Do not use md5(), sha1() or plain hash() for them.

Never store plain passwords.

If your database leaks, plain passwords are exposed at once. Hashes with a salt and a slow algorithm make each guess expensive.

Do not compare hashes with ==.

Each hash has its own salt, so two hashes of the same password differ. Always use password_verify().

PHP Syntax

The usual pattern: hash at sign-up, verify at login, rehash when settings are old:

PHP
// Sign-up: store this string in the database
$hash = password_hash($password, PASSWORD_DEFAULT);

// Login: true when the typed password matches
if (password_verify($typed, $hash)) {
    // Upgrade old hashes while you have the password
    if (password_needs_rehash($hash, PASSWORD_DEFAULT)) {
        $hash = password_hash($typed, PASSWORD_DEFAULT);
    }
}

$info  = password_get_info($hash);   // algo, algoName, options
$algos = password_algos();           // PHP 7.4+

Core Ideas

What to remember when you handle passwords:

IdeaFunction or constantRemember
Create a hashpassword_hash()Adds a random salt. A bcrypt hash is 60 characters long.
Check a loginpassword_verify()Pass the typed password and the stored hash. Returns a bool.
Upgrade old hashespassword_needs_rehash()Call it after a good login, then hash again if it returns true.
Read a hashpassword_get_info()Returns the algorithm, its name and the options such as cost.
See supported algorithmspassword_algos()Lists IDs such as 2y and argon2id (PHP 7.4+).
Default algorithmPASSWORD_DEFAULTBcrypt today. PHP may change it, so store hashes in 255 characters.
Other algorithmsPASSWORD_BCRYPT, PASSWORD_ARGON2IDPick one on purpose. Argon2 needs a PHP build with Argon2.
Old fast hashesmd5(), sha1()Too fast for passwords. Migrate users to password_hash().

A Safe Login Workflow

Most apps follow the same four steps. Each step maps to one function.

StepWhat happensFunction
1. Sign upHash the password and save the hashpassword_hash()
2. Log inCompare the typed password with the saved hashpassword_verify()
3. UpgradeRehash if the cost or algorithm is oldpassword_needs_rehash()
4. AuditCheck which algorithm and cost your hashes usepassword_get_info()
Set an explicit cost.

PHP 8.4 raised the default bcrypt cost from 10 to 12. If you need the same speed everywhere, pass ['cost' => 10] yourself.

PHP Examples

Three short scripts. None needs a database, and the output is the same every time you run it.

Example 1 — Sign Up and Log In

Hash a password, then check a right and a wrong attempt. The random hash is not printed.

PHP
<?php
$stored = password_hash("S3cret!pass", PASSWORD_BCRYPT, ["cost" => 10]);

foreach (["S3cret!pass", "guess"] as $typed) {
    echo $typed, ": ", password_verify($typed, $stored) ? "ok" : "denied", PHP_EOL;
}

How It Works

  1. Hash once. The result is stored, like in a database.
  2. Loop over two attempts. One is right, one is wrong.
  3. Call `password_verify()`. It uses the salt inside the hash.
  4. Print the result. Only the right password is ok.

Example 2 — Why md5() Is Not Enough

md5() gives the same output every time. password_hash() does not.

PHP
<?php
echo "md5 equal: ", md5("password") === md5("password") ? "yes" : "no", PHP_EOL;

$a = password_hash("password", PASSWORD_DEFAULT);
$b = password_hash("password", PASSWORD_DEFAULT);
echo "password_hash equal: ", $a === $b ? "yes" : "no", PHP_EOL;

How It Works

  1. Hash with md5 twice. The output is always the same.
  2. Know the risk. Attackers can use tables of common md5 values.
  3. Hash with password_hash() twice. The salts differ.
  4. Get different results. Precomputed tables no longer work.

Example 3 — Inspect and Upgrade a Hash

Read a stored hash, then decide if it needs a newer cost.

PHP
<?php
$hash = '$2y$07$BCryptRequires22Chrcte/VlQH0piJtjXl.0t1XkA8pw9dMXTpOq';
$info = password_get_info($hash);

echo $info['algoName'], " cost ", $info['options']['cost'], PHP_EOL;
echo password_needs_rehash($hash, PASSWORD_BCRYPT, ['cost' => 10]) ? "Upgrade it" : "Keep it", PHP_EOL;

How It Works

  1. Read the hash. password_get_info() finds bcrypt and cost 7.
  2. State the target. Cost 10 is the goal.
  3. Ask `password_needs_rehash()`. The costs differ, so it is true.
  4. Rehash at login. You need the plain password for that.

Next: PHP Password Functions Index

After you know hashing, verifying and upgrading, open the functions index for all five guides.

Start withWhy
password_hash()Create a salted hash to store in your database
password_verify()Check a typed password against a stored hash
password_needs_rehash()Find hashes that need newer settings
password_get_info()Read the algorithm and cost of a hash
password_algos()List the algorithms your PHP supports

Full list: PHP Password Functions index (/php/password/function).

Frequently Asked Questions

Call password_hash($password, PASSWORD_DEFAULT) and save the returned string. Never save the plain password. At login, use password_verify() with the typed password and the saved hash.
md5() and sha1() are fast and have no salt, so attackers can test huge numbers of guesses. password_hash() is slow on purpose and adds a random salt to every hash.
A constant that means the recommended algorithm. It is bcrypt today. PHP may switch it to a stronger one in a future version, so store hashes in a column of at least 255 characters.
A bcrypt hash is 60 characters. Other algorithms such as Argon2 produce longer strings, which is why 255 characters is the safe column size.
After a successful password_verify(), call password_needs_rehash(). If it returns true, create a new hash with password_hash() and save it in place of the old one.
You do not need your own salt. password_hash() creates one. A secret pepper is optional extra work and can be added by hashing with hash_hmac() first, but plain password_hash() is already a safe default.

Browse every Password function

Open the functions index for guides with examples and FAQs.

Password Functions Index →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful