How can developers securely store passwords in PHP scripts to prevent unauthorized access?
To securely store passwords in PHP scripts, developers should never store passwords in plain text. Instead, passwords should be hashed using a strong hashing algorithm like bcrypt. This ensures that even if the database is compromised, the passwords are not easily readable.
// Hashing a password using bcrypt
$password = "secret_password";
$hashed_password = password_hash($password, PASSWORD_BCRYPT);
// Verifying a hashed password
$entered_password = "secret_password";
if (password_verify($entered_password, $hashed_password)) {
echo "Password is correct!";
} else {
echo "Password is incorrect!";
}