How can PHP be used to track and manage user activity, such as determining if a user has been active for more than 15 minutes?

To track and manage user activity, such as determining if a user has been active for more than 15 minutes, you can use PHP session variables and timestamps. By storing the timestamp of the user's last activity in a session variable and comparing it to the current time, you can easily determine if the user has been inactive for more than 15 minutes.

// Start the session
session_start();

// Check if the user has an active session
if(isset($_SESSION['last_activity'])) {
    // Get the current time
    $current_time = time();

    // Get the timestamp of the user's last activity
    $last_activity = $_SESSION['last_activity'];

    // Check if the user has been inactive for more than 15 minutes
    if(($current_time - $last_activity) > 900) { // 900 seconds = 15 minutes
        // User has been inactive for more than 15 minutes, perform actions here
    }

    // Update the user's last activity timestamp
    $_SESSION['last_activity'] = $current_time;
} else {
    // Set the initial last activity timestamp
    $_SESSION['last_activity'] = time();
}