Skip to content

Instantly share code, notes, and snippets.

@vordan
Created December 27, 2021 20:35
Show Gist options
  • Save vordan/54cc25262108ffd7b2c80c929dbc6650 to your computer and use it in GitHub Desktop.
Save vordan/54cc25262108ffd7b2c80c929dbc6650 to your computer and use it in GitHub Desktop.
Generate and restore password hash #php #password #hash
// Save a password hash:
// ======================
$options = [
'cost' => 11,
];
// Get the password from post
$passwordFromPost = $_POST['password'];
$hash = password_hash($passwordFromPost, PASSWORD_BCRYPT, $options);
// Now insert it (with login or whatever) into your database, use mysqli or pdo!
// Get the password hash:
// ======================
// Get the password from the database and compare it to a variable (for example post)
$passwordFromPost = $_POST['password'];
$hashedPasswordFromDB = ...;
if (password_verify($passwordFromPost, $hashedPasswordFromDB)) {
echo 'Password is valid!';
}
else {
echo 'Invalid password.';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment