What alternative methods can be used to store and retrieve user login credentials in PHP for better security and performance?

Storing user login credentials securely is crucial for maintaining the security of a website. One common method is to hash the passwords before storing them in the database using a strong hashing algorithm like bcrypt. This ensures that even if the database is compromised, the passwords are not easily readable. Additionally, using prepared statements to interact with the database helps prevent SQL injection attacks.

// Hashing and storing user password
$password = $_POST['password'];
$hashed_password = password_hash($password, PASSWORD_BCRYPT);

// Storing hashed password in the database
$stmt = $pdo->prepare("INSERT INTO users (username, password) VALUES (:username, :password)");
$stmt->bindParam(':username', $_POST['username']);
$stmt->bindParam(':password', $hashed_password);
$stmt->execute();

// Retrieving and verifying user password
$entered_password = $_POST['password'];
$stmt = $pdo->prepare("SELECT password FROM users WHERE username = :username");
$stmt->bindParam(':username', $_POST['username']);
$stmt->execute();
$user = $stmt->fetch();

if ($user && password_verify($entered_password, $user['password'])) {
    // Password is correct
} else {
    // Password is incorrect
}