Page 1 of 1

online users

Posted: Sun Nov 16, 2008 11:04 am
by m2babaey
Hi
what is the best (and simple enough) idea to show online users? (how many and who)
thank you

Re: online users

Posted: Mon Nov 17, 2008 12:09 am
by XaeroDegreaz
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.

Code: Select all

 
mysql_query("UPDATE `users` SET `time_stamp` = ".time()." WHERE `username` = '".$username."' LIMIT 1");
 
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

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 />");
}
 
To show a number of how many users:

Code: Select all

 
$number_of_users = mysql_num_rows($query);
 
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.