What are some methods for monitoring the number of online clients in a network using PHP?

To monitor the number of online clients in a network using PHP, you can use a session-based approach where each client's session is stored and updated in a database or file. By checking the last activity time of each session, you can determine which clients are currently online.

// Start or resume a session
session_start();

// Update the last activity time for the current session
$_SESSION['last_activity'] = time();

// Check all active sessions to determine online clients
$active_clients = 0;
$max_inactive_time = 300; // 5 minutes of inactivity
foreach ($_SESSION as $key => $value) {
    if ($key !== 'last_activity' && (time() - $value['last_activity']) <= $max_inactive_time) {
        $active_clients++;
    }
}

echo "Number of online clients: " . $active_clients;