What are the advantages of using a database to store login attempt counts in PHP, as opposed to other methods?
Storing login attempt counts in a database allows for persistent storage of the data, making it more reliable and secure compared to storing it in sessions or cookies. It also allows for easier tracking and monitoring of login attempts over time. Additionally, using a database makes it easier to implement features like resetting the login attempt count after a certain period of time or locking an account after a certain number of failed attempts.
// Connect to the database
$pdo = new PDO('mysql:host=localhost;dbname=your_database', 'username', 'password');
// Increment login attempt count for a specific user
$user_id = 123;
$stmt = $pdo->prepare("UPDATE users SET login_attempts = login_attempts + 1 WHERE id = :user_id");
$stmt->bindParam(':user_id', $user_id);
$stmt->execute();