SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Zealot websalacarta's Avatar
    Join Date
    May 2003
    Location
    Spain
    Posts
    165
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    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.

  2. #2
    SitePoint Enthusiast
    Join Date
    Apr 2004
    Location
    country
    Posts
    82
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    it can't be real time...

  3. #3
    SitePoint Wizard mark_W's Avatar
    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'"
    You can use mysql_affected_rows to determine how many rows where returned.

    PHP Code:
    $usersonline mysql_affected_rows($sql); 
    It will only be updated as you refresh the page though.

  4. #4
    Level 8 Chinese guy Archbob's Avatar
    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>";
    ?>

  5. #5
    Non-Member coo_t2's Avatar
    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.

  6. #6
    SitePoint Zealot websalacarta's Avatar
    Join Date
    May 2003
    Location
    Spain
    Posts
    165
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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.

  7. #7
    SitePoint Wizard cranial-bore's Avatar
    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.
    mikehealy.com.au
    diigital.com art, design . Latest Work — Saturday Morning

  8. #8
    Level 8 Chinese guy Archbob's Avatar
    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •