online users

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
m2babaey
Forum Contributor
Posts: 364
Joined: Sun May 20, 2007 9:26 am

online users

Post by m2babaey »

Hi
what is the best (and simple enough) idea to show online users? (how many and who)
thank you
XaeroDegreaz
Forum Newbie
Posts: 10
Joined: Mon Nov 10, 2008 9:09 pm

Re: online users

Post 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.
Post Reply