What are some best practices for ensuring the security of a PHP login system?

Issue: One common vulnerability in PHP login systems is SQL injection attacks, where malicious users can manipulate SQL queries to gain unauthorized access. To prevent this, it is essential to use parameterized queries and sanitize user input. Code snippet:

// Using parameterized queries to prevent SQL injection
$stmt = $pdo->prepare('SELECT * FROM users WHERE username = :username AND password = :password');
$stmt->bindParam(':username', $username);
$stmt->bindParam(':password', $password);
$stmt->execute();

// Sanitizing user input to prevent XSS attacks
$username = htmlspecialchars($_POST['username']);
$password = htmlspecialchars($_POST['password']);