How can PHP developers prevent brute force attacks on login systems?

To prevent brute force attacks on login systems, PHP developers can implement measures such as rate limiting, CAPTCHA challenges, and account lockouts after multiple failed login attempts.

// Implementing rate limiting to prevent brute force attacks
$login_attempts = 3; // Number of allowed login attempts
$lockout_duration = 5; // Lockout duration in minutes
$ip_address = $_SERVER['REMOTE_ADDR'];

if ($_SESSION['login_attempts'][$ip_address] >= $login_attempts) {
    // Implement account lockout logic
    $_SESSION['lockout'][$ip_address] = time() + ($lockout_duration * 60);
    die('Account locked. Please try again later.');
}

// Validate login credentials
if ($valid_credentials) {
    // Successful login, reset login attempts
    unset($_SESSION['login_attempts'][$ip_address]);
} else {
    // Failed login attempt, increment counter
    $_SESSION['login_attempts'][$ip_address] = ($_SESSION['login_attempts'][$ip_address] ?? 0) + 1;
}