What are the best practices for handling user authentication in PHP to allow multiple users to log in successfully?

When handling user authentication in PHP to allow multiple users to log in successfully, it is recommended to use a secure hashing algorithm like bcrypt to store passwords in the database. Additionally, it is important to use prepared statements to prevent SQL injection attacks. Finally, consider implementing features like account lockout after multiple failed login attempts to enhance security.

// Sample code for user authentication in PHP
// Assuming $username and $password are obtained from user input

// Connect to the database
$pdo = new PDO('mysql:host=localhost;dbname=database', 'username', 'password');

// Prepare the SQL statement
$stmt = $pdo->prepare("SELECT * FROM users WHERE username = :username");
$stmt->execute(['username' => $username]);
$user = $stmt->fetch();

// Verify the password using password_verify
if ($user && password_verify($password, $user['password'])) {
    // Successful login
    echo "Login successful!";
} else {
    // Failed login attempt
    echo "Invalid username or password.";
}