What are the potential pitfalls of using hashed passwords in PHP for user authentication?
Using hashed passwords in PHP for user authentication is generally a good practice to secure user data. However, one potential pitfall is using weak hashing algorithms or not salting the passwords, which can make them vulnerable to brute force attacks. To mitigate this risk, it's important to use strong hashing algorithms like bcrypt and include a unique salt for each password.
// Hashing a password with bcrypt and a unique salt
$password = "password123";
$salt = uniqid(mt_rand(), true); // Generate a unique salt
$hashed_password = password_hash($password . $salt, PASSWORD_BCRYPT);
// Verifying a password
$user_input = "password123";
if (password_verify($user_input . $salt, $hashed_password)) {
echo "Password is correct";
} else {
echo "Password is incorrect";
}