PHP Password Hashing Guide

In PHP, you can utilize the password_hash() function to hash encrypt passwords. This function encrypts passwords using the bcrypt algorithm and returns a hash value.

Here is a simple example:

$password = '123456';
$hashedPassword = password_hash($password, PASSWORD_DEFAULT);

echo $hashedPassword;

In the example above, we are hashing the string ‘123456’ and storing the resulting hash value in the variable $hashedPassword.

If you need to check if the user-entered password matches the stored hash value, you can use the password_verify() function.

Here is an example of password verification:

$storedHash = '$2y$10$Kj2z4fOzvNH9t0v4mnVj4eU0GRc2tY1QoED7yLKwRsZhuNf9bM6gK'; // 从数据库中获取存储的哈希值

if (password_verify($password, $storedHash)) {
    echo '密码匹配';
} else {
    echo '密码不匹配';
}

In the example above, we will compare the stored hash value retrieved from the database with the user’s input password. If they match, it will output “password matched”, otherwise it will output “password not matched”.

bannerAds