What role does using timestamps play in managing user logins and logouts in PHP sessions?
Using timestamps in PHP sessions allows for tracking the time of user logins and logouts, which can be useful for managing session timeouts, tracking user activity, and enforcing security measures. By storing timestamps in session variables, you can easily check the duration of a user's session and automatically log them out after a certain period of inactivity.
// Start the session
session_start();
// Set the login timestamp when the user logs in
$_SESSION['login_time'] = time();
// Check if the user has been inactive for more than 30 minutes and log them out
if (isset($_SESSION['login_time']) && (time() - $_SESSION['login_time'] > 1800)) {
session_unset();
session_destroy();
// Redirect to login page or perform any other necessary action
}