How can PHP be used to handle user authentication and update last login dates efficiently?
To handle user authentication and update last login dates efficiently in PHP, you can create a function that checks the user's credentials against a database and updates the last login date if the authentication is successful. This function can be called whenever a user logs in to your application.
<?php
function authenticateUser($username, $password) {
// Check user credentials against database
// If authentication is successful, update last login date
if ($authenticated) {
// Update last login date in database
$lastLoginDate = date('Y-m-d H:i:s');
// Update last login date for user in database
$query = "UPDATE users SET last_login = '$lastLoginDate' WHERE username = '$username'";
// Execute the query
// Return true if authentication is successful
} else {
// Return false if authentication fails
}
}
// Call the function with user input
authenticateUser($_POST['username'], $_POST['password']);
?>