Hi, I need the 'online now' feature
Iīve seen it in phpbb and vbulletin forums
is it real or itīs refreshed each five minutes?
I mean, if user A disconnect, user B will see he online a few minutes?
thanks.
| SitePoint Sponsor |
Hi, I need the 'online now' feature
Iīve seen it in phpbb and vbulletin forums
is it real or itīs refreshed each five minutes?
I mean, if user A disconnect, user B will see he online a few minutes?
thanks.
it can't be real time...





Basically you could have an enum row in your users database that is set to either 1 or 0 depending if the user is currently logged in. Then pull a query that gets the logged in users by checking the enum row. Something like below.
You can use mysql_affected_rows to determine how many rows where returned.PHP Code:$sql = "SELECT * FROM users WHERE userloggedin = '1'";
It will only be updated as you refresh the page though.PHP Code:$usersonline = mysql_affected_rows($sql);





Here how I do it with my forum:
Code:<?php $recent=date("U")-900; $getusersonline="SELECT userID,username from b_users where lasttime>'$recent'"; //grab from sql users on in last 15 minutes $getusersonline2=mysql_query($getusersonline) or die("Could not get users"); $num=mysql_num_rows($getusersonline2); $countguests="SELECT DISTINCT guestip from guestsonline where time>'$recent'"; $countguests2=mysql_query($countguests) or die("Could not count guests"); $thecount=mysql_num_rows($countguests2); print "<table class='maintable' cellspacing='1'>"; print "<tr class='headline'><td colspan='2'><b>There have been $num members and $thecount guests online in the last 15 minutes</td></tr>"; print "<tr class='forumrow'><td>"; while($getusersonline3=mysql_fetch_array($getusersonline2)) { print "<A href='profile.php?userID=$getusersonline3[userID]'>$getusersonline3[username]</a>,"; } print "</td></tr></table><br><br>"; ?>





You can also count session files.
http://www.devarticles.com/c/a/PHP/T...line-With-PHP/
Whether you use a DB or count the session files, you should probably make a cron job that runs evey x(like every 5) minutes and just stores the static result in a file that you can include(), unless it's really important that you have live up-to-the-minute accuracy.
Last edited by coo_t2; Jul 7, 2005 at 21:25.
Hi, thanks for your help,
I donīt only need count users online, I also need to know wich users are online
(something similar to sitepoint forum, that says wich users are online)
ways to do it:
1) database
- each time the user A changes the page, the database is updated with the current time
- user B reads that value from database, and if it is < than 1 min from now, he will see user A online
The problem of this is: if user A is seeing the same page for a long time, he will not appear online for the rest of users after the first minute
2) database + AJAX
using AJAX, user A can update database each minute, inclusive if he doesnīt change the page he is viewing (it can be done automatically).
what do you think about it?
(sorry for my bad english)
thanks.





I don't think they are ever totally accurate, because if the browser window is closed the session won't be destroyed immediately and no server interaction will be triggered to change DB records.
So whatever method you go with is going to be dependant on how long you decide a user should be considered offline after their last activity.





They are never completely accurate as there is always room for error. THe code I posted also display which users are online.
Bookmarks