What is the purpose of storing timestamps in a session in PHP?

Storing timestamps in a session in PHP can be useful for tracking user activity, session expiration, or implementing timeout functionality. By storing timestamps in a session, you can easily compare current time with the timestamp stored in the session to determine if a certain amount of time has passed.

// Start the session
session_start();

// Store the current timestamp in the session
$_SESSION['timestamp'] = time();

// Check if a certain amount of time has passed since the timestamp was stored
if (isset($_SESSION['timestamp']) && (time() - $_SESSION['timestamp']) > 3600) {
    // Perform actions if the session has expired
}