How can PHP be used to save generated passwords in a text file and later read and verify them?
To save generated passwords in a text file using PHP, you can hash the passwords using a secure hashing algorithm like password_hash() and then write them to a text file. To later read and verify the passwords, you can read the hashed passwords from the text file, hash the input password using the same algorithm, and compare the hashed values using password_verify().
// Save generated passwords in a text file
$password = "secret_password";
$hashed_password = password_hash($password, PASSWORD_DEFAULT);
file_put_contents("passwords.txt", $hashed_password . PHP_EOL, FILE_APPEND);
// Read and verify passwords from the text file
$stored_passwords = file("passwords.txt", FILE_IGNORE_NEW_LINES);
$input_password = "secret_password";
foreach ($stored_passwords as $stored_password) {
if (password_verify($input_password, $stored_password)) {
echo "Password verified!";
break;
}
}
Related Questions
- How can the code structure and readability be improved in the PHP poll script to avoid confusion and potential errors?
- In what scenarios is it necessary to use session variables in PHP, and when can form data be accessed directly without session storage?
- How can CSS styles be applied to a specific placeholder (%bild%) in a PHP template?