Free tutorials & notes in Hindi & English · Clean code examples 🧰 Free PDF & Image Tools →
🔴 Advanced  ·  Lesson 33

Password Hashing in PHP

In this lesson: Store passwords safely in PHP with password_hash and password_verify — why not md5, rehash and login example. Secure it now.

Never store plain passwords

The single most important rule of login security: never store a password as plain text. If your database is ever stolen, plain passwords are handed straight to the attacker. Instead you store a hash — a scrambled, one-way version — using PHP's built-in functions, which are both easy and very strong.

⚠️ Store the hash, never the real password. You verify against the hash; you never turn it back.

Hashing a password

When a user signs up, hash their password before saving it.

<?php
  $password = "mySecret123";
  $hash = password_hash($password, PASSWORD_DEFAULT);
  echo $hash;   // a long scrambled string
?>
$2y$10$Ku3...long...hash

password_hash uses a strong algorithm (bcrypt by default) and adds a random salt automatically. Save this hash in your users table — not the original password.

Verifying at login

At login, compare the entered password to the stored hash.

<?php
  $entered = "mySecret123";
  $storedHash = "$2y$10$Ku3...";   // from the database

  if (password_verify($entered, $storedHash)) {
      echo "Login success";
  } else {
      echo "Wrong password";
  }
?>
Login success

password_verify checks the entered password against the hash and returns true or false. You never decrypt the hash — you only verify against it.

Why hashing is safe

A hash is one-way: you can turn a password into a hash, but not a hash back into the password. So even if attackers steal your database, they only get scrambled hashes, not usable passwords. The automatic random salt also means two users with the same password get different hashes, defeating precomputed attack tables.

Keeping hashes up to date

<?php
  if (password_needs_rehash($storedHash, PASSWORD_DEFAULT)) {
      $newHash = password_hash($entered, PASSWORD_DEFAULT);
      // save $newHash to the database
  }
?>

As PHP improves its default algorithm, password_needs_rehash lets you upgrade old hashes automatically at the next successful login — keeping security current with no user effort.

Common mistakes

  • Storing plain passwords. The classic, dangerous mistake — always hash.
  • Using old functions like md5 or sha1. These are not safe for passwords; use password_hash.
  • Trying to decrypt the hash. Hashes are one-way — verify with password_verify instead.
  • Adding your own weak salt. Not needed — password_hash salts for you.

Summary

  • Never store plain passwords — always store a hash.
  • Hash with password_hash($pw, PASSWORD_DEFAULT).
  • Check logins with password_verify($entered, $hash).
  • Hashes are one-way and automatically salted — very safe.
  • Use password_needs_rehash to keep hashes current.

Next, build the full flow in login and registration, or protect the database with prepared statements.

Frequently Asked Questions

Why should I never store passwords as plain text?
If your database is ever leaked, plain passwords are instantly exposed — and since people reuse passwords, their other accounts are at risk too. Hashing scrambles the password so even a leaked database does not reveal it.
What does password_hash do?
password_hash($password, PASSWORD_DEFAULT) turns a password into a long, scrambled hash using a strong algorithm (currently bcrypt). You store this hash, never the original password.
How do I check a password at login?
Use password_verify($entered, $storedHash). It returns true if the entered password matches the stored hash, and false if not. You never "decrypt" the hash — you verify against it.
Can I reverse a hash back to the password?
No. A good hash is one-way — you cannot turn it back into the original password. That is exactly why it is safe: even you (or an attacker) cannot read the real password from the stored hash.
Do I need to add my own salt?
No. password_hash automatically adds a unique random salt to every hash, so two users with the same password get different hashes. You do not manage the salt yourself.
← Back to PHP Tutorial
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

💻 Live Code Editor

This page's programs are ready here — run them, edit them, and learn. No installation needed.
Powered by OneCompiler. The code loads into the editor automatically — press Run to see the output. If the editor does not open, open it in a new tab.
Next topicPagination Your progress: 33 / 50 (66%) · Lesson 34 / 50 Continue →

How was this lesson?

Your feedback helps us make the content better.

Write a review or suggestion (optional)

After approval, your name and city may appear publicly with your review. Please do not write your email or phone number.

▶ CodingEasily on YouTube

Learn with video lessons in easy Hindi

Featured video: RDBMS Part 3 – Class XI Commerce IT

▶ Subscribe – it’s free