What are the best practices for encrypting and storing passwords in a PHP login system to enhance security?
To enhance security in a PHP login system, it is essential to encrypt and securely store passwords. One of the best practices is to use a strong hashing algorithm like bcrypt to hash passwords before storing them in the database. Additionally, it is recommended to use unique salt for each password to prevent rainbow table attacks and to periodically rehash passwords for added security.
// Hashing and storing password securely
$password = 'user_password';
$hashedPassword = password_hash($password, PASSWORD_BCRYPT);
// Verifying hashed password
if (password_verify($password, $hashedPassword)) {
echo 'Password is valid!';
} else {
echo 'Invalid password.';
}