Hi
what is the best (and simple enough) idea to show online users? (how many and who)
thank you
online users
Moderator: General Moderators
-
XaeroDegreaz
- Forum Newbie
- Posts: 10
- Joined: Mon Nov 10, 2008 9:09 pm
Re: online users
I would start by adding a new field in your user database that is a timestamp (you can leave it as an INT field in the db).
Then, on each page that you have that a user must be logged into, at the top of your script, add a mysql operation that updates that user's timestamp field.
That's the part that enables the server to track the user's page moving time.
Example of how to display a list of 5 users that are online
To show a number of how many users:
That's a really rough example, but it will work. It's up to you to make it SAFE for use and also to make it sparkle.
Then, on each page that you have that a user must be logged into, at the top of your script, add a mysql operation that updates that user's timestamp field.
Code: Select all
mysql_query("UPDATE `users` SET `time_stamp` = ".time()." WHERE `username` = '".$username."' LIMIT 1");
Example of how to display a list of 5 users that are online
Code: Select all
$active_time_limit = time()-300 //# 5 minutes...
$limit = 5;
$query = mysql_query("SELECT `username` FROM `users` WHERE `time_stamp` >= $active_time_limit ORDER BY `username` LIMIT $limit");
//# Loop through all of the results
while ($user = mysql_fetch_assoc($query)) {
echo($user['username']."<br />");
}
Code: Select all
$number_of_users = mysql_num_rows($query);