How to count live users ?

Discussion in 'PHP' started by qualityhostings, Jul 24, 2009.

  1. #1
    Hello

    Just curious to know, how can we count the online users visiting a page using php ?
     
    qualityhostings, Jul 24, 2009 IP
  2. jestep

    jestep Prominent Member

    Messages:
    3,659
    Likes Received:
    215
    Best Answers:
    19
    Trophy Points:
    330
    #2
    PHP does not have any asyncronous ability, so there would be no way to check which users are active on a page.

    There are a few ways to do it. The easiest would be to add a user's session and time to a database table with each page. You would then have php look at how many users were on the page in the last 30 seconds or so. You could also call this with an Ajax request and auto-update ever few seconds, for a running count.
     
    jestep, Jul 24, 2009 IP
  3. kblessinggr

    kblessinggr Peon

    Messages:
    539
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Basically what jestep said, Sessions + possibly MySQL (upon each request you can sanitize the database by removing users with a last-known-activity older than say 5, 10, 15 minutes etc.) Course if you go the AJAX route, you can have that update the database ever 5 or 10 seconds for a tighter representation of who's on.

    Using JQuery's $.get() or $.load() would work well in this case if you make the php simply echo back a number.
     
    kblessinggr, Jul 24, 2009 IP
  4. bncplix

    bncplix Banned

    Messages:
    76
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Store the sessions each time the page is loaded
    Assume that after a set time, like 5 minutes they are off the page, but once they go to a new page or refresh, tht is readded in

    Use mysql to store he info, I already coded one, I can sell it to you if you like.

    Just let me know
     
    bncplix, Jul 24, 2009 IP
  5. techbongo

    techbongo Active Member

    Messages:
    309
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    80
    #5
    However, if you use ajax with php, your work will be easier.
     
    techbongo, Jul 24, 2009 IP