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;
    }
}