How can PHP developers securely handle password authentication using text files instead of a database?
When handling password authentication using text files instead of a database, PHP developers can securely store passwords by hashing them before saving them in the text file. This adds an extra layer of security by ensuring that plaintext passwords are not stored. Additionally, developers should use a secure hashing algorithm like bcrypt and salt the passwords before hashing to further enhance security.
// Hash and save password to text file
$password = "secret_password";
$salt = "random_salt_here";
$hashed_password = password_hash($password . $salt, PASSWORD_BCRYPT);
file_put_contents('passwords.txt', $hashed_password . PHP_EOL, FILE_APPEND);
// Verify password from text file
$stored_password = file_get_contents('passwords.txt');
$entered_password = "secret_password";
if (password_verify($entered_password . $salt, $stored_password)) {
echo "Password is correct!";
} else {
echo "Password is incorrect!";
}