How can a PHP developer prevent SQL injection vulnerabilities in a login system?

To prevent SQL injection vulnerabilities in a login system, PHP developers should use prepared statements with parameterized queries instead of directly inserting user input into SQL queries. This helps to separate SQL logic from user input, preventing malicious SQL injection attacks.

// Establish a database connection
$pdo = new PDO('mysql:host=localhost;dbname=database_name', 'username', 'password');

// Prepare a SQL statement with placeholders for user input
$stmt = $pdo->prepare('SELECT * FROM users WHERE username = :username AND password = :password');

// Bind user input to the placeholders
$stmt->bindParam(':username', $_POST['username']);
$stmt->bindParam(':password', $_POST['password']);

// Execute the prepared statement
$stmt->execute();

// Fetch the results
$user = $stmt->fetch();

// Check if the user exists and handle the login process accordingly
if ($user) {
    // User exists, proceed with login
} else {
    // User does not exist or invalid credentials
}