Real-time chat systems have become an essential part of modern social platforms. Since PHP is one of the most popular web programming languages, this article will introduce how to implement online status display and user count tracking features in a real-time chat system using PHP. We will explain in detail how to manage online statuses using PHP's Session mechanism and how to track online users using timers.
Online status display is an important metric in a chat system that indicates whether users are online. In the chat interface, users typically want to see if the person they are communicating with is online and receive a quick reply. Implementing this feature requires using PHP's Session mechanism.
Sessions are a protocol used in web programming to store user information. With Sessions, web applications can store user-related data on the server side, instead of saving this data in the user's browser cookies. This allows the Session to share information between multiple pages, effectively managing the user's session state.
When a user logs into the chat system, the server starts a Session and records the login time. Every time the user makes a request, the system checks if the Session has expired. If the Session is expired, the user is marked as offline and their online status is deleted from the database. If the Session has not expired, the system updates the user's online status to reflect they are still online.
Here is an example of the code for implementing the online status display feature:
session_start(); // Start the Session if (isset($_SESSION['last_activity']) && (time() - $_SESSION['last_activity'] > 60)) { // User is offline $user_id = $_SESSION['user_id']; // Remove user status from database $query = "DELETE FROM `users_online` WHERE `user_id` = '$user_id'"; $result = mysqli_query($link, $query); // Clear Session session_unset(); session_destroy(); } else { // User is still online $user_id = $_SESSION['user_id']; // Update user status $query = "UPDATE `users_online` SET `last_activity` = NOW() WHERE `user_id` = '$user_id'"; $result = mysqli_query($link, $query); }
Tracking the number of online users is another important feature in a chat system. The user count feature relies on PHP's Timer mechanism, which allows periodic queries to track the number of online users.
A timer is a mechanism that executes code at specific time intervals. PHP's timers run scripts based on predefined intervals, automatically executing when the time is reached.
We can use PHP's timers to periodically query the online user table and calculate the current number of online users. Below is an example of the Heartbeat timer for tracking user count:
set_time_limit(0); // Use PHP Timer while(true) { $query = "SELECT COUNT(*) as online_users FROM `users_online` WHERE `last_activity` > DATE_SUB(NOW(), INTERVAL 1 MINUTE)"; $result = mysqli_query($link, $query); $online_users = mysqli_fetch_array($result, MYSQLI_ASSOC)['online_users']; echo "Current online users: " . $online_users; ob_flush(); flush(); sleep(10); // Timer interval set to 10 seconds }
In this example, we use an infinite loop to continuously poll the user table. The functions ob_flush() and flush() are used to output the buffer and print the output. Finally, we use the sleep() function to pause the program for 10 seconds to ensure the timer has enough time to query the online user table.
Online status display and user count tracking are essential features for every chat system. In this article, we introduced how to implement these features using PHP. We used the Session mechanism to manage online status and the Timer mechanism to track the number of online users. With these technologies, developers can efficiently manage online users and provide important real-time feedback to users.