How can PHP utilize file modification time to track user activity and online status in a chat application?

To track user activity and online status in a chat application using file modification time, we can update a user's "last seen" timestamp in a file whenever they perform an action in the chat. By checking the difference between the current time and the file's modification time, we can determine if the user is online or offline based on a specified time threshold.

// Update user's last seen timestamp in a file
$user_id = 123;
$last_seen_file = "last_seen_$user_id.txt";
file_put_contents($last_seen_file, time());

// Check if user is online based on a time threshold
$threshold = 60; // 1 minute threshold
$last_modified_time = filemtime($last_seen_file);
$current_time = time();

if ($current_time - $last_modified_time <= $threshold) {
    echo "User is online";
} else {
    echo "User is offline";
}