How can PHP sessions be integrated with automatic online status updates in a web application?

To integrate PHP sessions with automatic online status updates in a web application, you can set a session variable to store the user's last activity timestamp. Then, you can check this timestamp against the current time to determine if the user is online or offline.

// Start the session
session_start();

// Set the last activity timestamp in the session
$_SESSION['last_activity'] = time();

// Check if the user is online based on their last activity within a certain time frame
$online_threshold = 60; // 1 minute
if(isset($_SESSION['last_activity']) && (time() - $_SESSION['last_activity'] <= $online_threshold)) {
    echo "User is online";
} else {
    echo "User is offline";
}