SitePoint Sponsor |
|
User Tag List
Results 1 to 8 of 8
Thread: online now feature
-
Jul 7, 2005, 14:56 #1
online now feature
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.
-
Jul 7, 2005, 15:03 #2
- Join Date
- Apr 2004
- Location
- country
- Posts
- 82
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
it can't be real time...
-
Jul 7, 2005, 15:11 #3
- Join Date
- Mar 2004
- Location
- West Midlands, United Kingdom
- Posts
- 2,631
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
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.
PHP Code:$sql = "SELECT * FROM users WHERE userloggedin = '1'";
PHP Code:$usersonline = mysql_affected_rows($sql);
-
Jul 7, 2005, 15:58 #4
- Join Date
- Sep 2001
- Location
- Somewhere in this vast universe
- Posts
- 3,741
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
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>"; ?>
-
Jul 7, 2005, 16:18 #5
- Join Date
- Feb 2003
- Location
- Dog Street
- Posts
- 1,819
- Mentioned
- 1 Post(s)
- Tagged
- 1 Thread(s)
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.
-
Jul 8, 2005, 02:02 #6
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.
-
Jul 8, 2005, 02:48 #7
- Join Date
- Jan 2002
- Location
- Australia
- Posts
- 2,634
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
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.
-
Jul 8, 2005, 09:11 #8
- Join Date
- Sep 2001
- Location
- Somewhere in this vast universe
- Posts
- 3,741
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
They are never completely accurate as there is always room for error. THe code I posted also display which users are online.
Bookmarks