What is the common practice for displaying online users on a PHP website?
When displaying online users on a PHP website, a common practice is to store the user's last activity timestamp in a database and then query the database for users who have been active within a certain timeframe. This allows you to accurately display online users in real-time.
// Assuming you have a 'users' table with a column 'last_activity' storing timestamps
// Query the database for users active within the last 5 minutes
$active_users_query = "SELECT * FROM users WHERE last_activity >= NOW() - INTERVAL 5 MINUTE";
$active_users_result = mysqli_query($connection, $active_users_query);
// Display the online users
while($row = mysqli_fetch_assoc($active_users_result)) {
echo $row['username'] . " is online<br>";
}