How can PHP be used to securely store passwords and corresponding login names?

To securely store passwords and corresponding login names in PHP, it is recommended to use password hashing functions like password_hash() and password_verify(). These functions help to securely hash passwords before storing them in a database, making it difficult for attackers to retrieve the original password. When a user logs in, their entered password can be verified against the hashed password using password_verify().

// Hashing and storing a password
$password = "user_password";
$hashed_password = password_hash($password, PASSWORD_DEFAULT);
$username = "user_name";

// Storing the username and hashed password in a database
// INSERT INTO users (username, password) VALUES ('$username', '$hashed_password');

// Verifying a login attempt
$login_username = "user_name";
$login_password = "user_password";

// Retrieve the hashed password from the database based on the username
// SELECT password FROM users WHERE username = '$login_username'

// Verify the login password
if (password_verify($login_password, $hashed_password)) {
    echo "Login successful!";
} else {
    echo "Login failed. Please try again.";
}