Created
December 27, 2021 20:35
-
-
Save vordan/54cc25262108ffd7b2c80c929dbc6650 to your computer and use it in GitHub Desktop.
Generate and restore password hash #php #password #hash
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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