What are some best practices for securely managing user passwords in PHP applications, especially when storing them in text-based files?

Storing user passwords in text-based files can pose a security risk as the passwords are easily accessible if the file is compromised. To securely manage user passwords in PHP applications, it is recommended to hash the passwords using a strong hashing algorithm like bcrypt before storing them in the file. This adds an extra layer of security by ensuring that even if the file is accessed, the passwords cannot be easily decrypted.

// Hashing and storing a user password in a text-based file
$password = "user_password";
$hashed_password = password_hash($password, PASSWORD_BCRYPT);

$file = "passwords.txt";
file_put_contents($file, $hashed_password . PHP_EOL, FILE_APPEND);