What are common issues that can arise when using PHP for login scripts?

One common issue that can arise when using PHP for login scripts is the vulnerability to SQL injection attacks if user input is not properly sanitized. To prevent this, it is important to use prepared statements or parameterized queries when interacting with the database.

// Using prepared statements to prevent SQL injection

// Assuming $username and $password are user inputs
$stmt = $pdo->prepare("SELECT * FROM users WHERE username = :username AND password = :password");
$stmt->execute(['username' => $username, 'password' => $password]);

// Check if user exists and password is correct
$user = $stmt->fetch();
if ($user) {
    // Login successful
} else {
    // Login failed
}