×

php · level 3 · unit 5 · lesson 3

  1. meet it
  2. guess first
  3. your turn
  4. fix it
  5. nailed it

Tap a green word if you get lost.

meet it

Store a of the password, never the password itself.

If your database leaks, every plain password leaks with it, and people reuse passwords everywhere. So never store them. Store a hash instead: a scrambled fingerprint that cannot be turned back. password_hash($password, PASSWORD_DEFAULT) makes one with a slow, strong method chosen by PHP.

Each hash includes a random , so two people with the same password still get different hashes, and an attacker cannot look them up in a ready-made list. To check a login, password_verify($typed, $hash) repeats the scrambling and returns true or false.

<?php
$hash = password_hash("correct horse", PASSWORD_DEFAULT);
echo "Stored: " . strlen($hash) . " characters\n";
if (password_verify("correct horse", $hash)) {
    echo "Right password: welcome back!\n";
}
if (!password_verify("wrong horse", $hash)) {
    echo "Wrong password: try again.\n";
}

The hash itself changes on every run because of its salt, so these lessons show its length instead.