📅  最后修改于: 2023-12-03 15:33:25.353000             🧑  作者: Mango
Password hash is a PHP function that can be used to securely hash passwords. It was introduced in PHP 5.5, and provides a simple way to generate a strong, salted and hashed password hash, which can be stored in a database to verify the user's password upon login.
The syntax for using password hash is:
password_hash ($password, $algorithm, $options)
where:
$password
: is the plain text password that you want to hash.$algorithm
: is the hashing algorithm to use. It can be one of the following constants: PASSWORD_DEFAULT, PASSWORD_BCRYPT, PASSWORD_ARGON2I or PASSWORD_ARGON2ID.$options
: is an array of options for the hashing algorithm. It is optional and depends on the algorithm used.Here is an example of how to use password hash to create a hash for a password:
// Plain text password
$password = 'mySecretPassword';
// Hash the password with the default algorithm
$hash = password_hash($password, PASSWORD_DEFAULT);
// Store the hash in the database
$sql = "INSERT INTO users (username, password_hash) VALUES ('$username', '$hash')";
To verify a password hash, you can use the password_verify
function, which takes the plain text password and the password hash as inputs and returns a boolean value indicating whether the password is correct.
Here is an example of how to verify a password hash:
// Plain text password
$password = 'mySecretPassword';
// Stored hash in the database
$hash = '$2y$10$XiFZAHLym/teXMN1JcY75eKVX.MHx2vLJgDdcptucnlbv4J4sKM2O';
// Verify the password hash
if (password_verify($password, $hash)) {
echo 'Password is valid!';
} else {
echo 'Password is invalid!';
}
Password hash is a simple and secure way of hashing passwords in PHP. It provides a standardized way of generating strong, salted and hashed password hashes, which can be stored in a database and verified upon login. It is recommended to use password hash for all password related tasks in PHP.