SitePoint Sponsor

User Tag List

Results 1 to 19 of 19
  1. #1
    SitePoint Zealot Beckwith's Avatar
    Join Date
    Mar 2004
    Location
    California
    Posts
    149
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Users Online...I'm confused

    I know that when you're trying to count how many users are online, you simply insert into a database the time they logged on, but how do you know when they've logged out? I don't seem to understand how that's possible, because I thought php scripts are run only when someone requests the page...but if they close the browser, how does the php script know when to remove their entry from the database? I'm so confused on how that's possible...enlighten me

  2. #2
    SitePoint Wizard swdev's Avatar
    Join Date
    Oct 2004
    Location
    UK
    Posts
    1,053
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ypu are quite correct. The only time that you know for certain that soemone has logged out of your site is if they go through your logout procedure.

    What mose sites do is to hold the date/time they last viewed a page (any page) on their website. Then, if that last viewed time was more than, say, 1 hour ago, they ASSUME that this user is no longer on their site, so the site logs them out.
    That 1 hour is for example only - change as required.

    Hope that makes things a little clearer

  3. #3
    SitePoint Addict launchcode's Avatar
    Join Date
    Dec 2004
    Location
    Bristol, UK
    Posts
    259
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    but if they close the browser, how does the php script know when to remove their entry from the database?
    If that is how they leave your site (and many do!) then the simple answer is - you can't! So you make a best guess based on activity and how long you think a typical user would remain on your site.

    Record when they login, or when they access a page, whatever you need - if they actually bother to logout then you can discount those entries. Otherwise if the average time spent on your site according to your log files is say 15 minutes then just count all the activity in the past 15 minutes.

    Cheers,

    Rich
    Richard Davey

    Launchcode
    PHP Security Guide. Think your scripts are secure? Think again.

  4. #4
    SitePoint Wizard Sillysoft's Avatar
    Join Date
    May 2002
    Location
    United States :)
    Posts
    1,691
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Or grab their information everytime they hit a page,IE their ip address, then do a calc that says if person has not hit the page in the last so many seconds or minutes, then delete all of their entries. Then do a DISTINCT on their ip address and that will get you the number of users online.

    Silly

  5. #5
    SitePoint Zealot Beckwith's Avatar
    Join Date
    Mar 2004
    Location
    California
    Posts
    149
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    well I think I understand all of that, but the main problem I'm having trouble understanding is, what if they're viewing the site for a minute, and I have it set to log them out at 2, and then they close the browser at 1, and the php script is no longer, therefore it would keep them online wouldn't it? Are the php scripts running all the time or what? ahh so confused lol

  6. #6
    SitePoint Evangelist
    Join Date
    Mar 2003
    Location
    Melbourne, Australia
    Posts
    463
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    this is what we do
    we have a seperate table that keeps the user's id, and the time that their login expires
    on every page there is a function that checks the time, and compares with that table
    if the time is greater than the expiry time, just remove all entries from that table

    but if the user is still active, then you increment their expiry time

    hope that makes sense
    dave

  7. #7
    SitePoint Zealot Beckwith's Avatar
    Join Date
    Mar 2004
    Location
    California
    Posts
    149
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    yeah I understand that part completely, the part I'm confused with is how you can do it when the user has terminated the page...I thought php was simple send request to the server, server spits back out html and that was it. What you're saying here, though, is that the script is constantly running and can be running in the background, so when the user closes the page, it'll still be running for a while because how else would it be able to change the database?

    I totally understand how it's done, I'm just confused with how it can be done because this isn't clientside javascript...do any of you have an explanation?

  8. #8
    SitePoint Evangelist
    Join Date
    Mar 2003
    Location
    Melbourne, Australia
    Posts
    463
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    well, our system works good enough for us
    it works because, every page has the script, and we get lots of visitors
    sure, it only runs if there is activity, but that's all we need atm

    you can if you want, and have the permissions
    write a php script that runs as a cron job, and runs every 15 minutes or whatever
    that checks user activity in the same way

  9. #9
    SitePoint Addict
    Join Date
    Oct 2004
    Location
    USA, Saratoga Springs, NY
    Posts
    296
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP scripts are only processed when a user accesses a page on your domain that has an extension that the webserver has been told to access the PHP engine with (i.e.: *.php, *.php3, *.php4). Therefore, if they're viewing the page that was written in PHP, it has already been executed, and the user is viewing the RESULT from that execution. The page is not running all the time, only when a REQUEST is made for that page. As soon as a user begins to see the results from the page, the process is (almost always) already completed.

    The reason PHP cannot identify when a user leaves your site, is because they are not telling your site specifically that "Hey, I'm leaving...you can lock up now." They just walk out the door without letting anyone know. It's like having a huge birthday party and someone gets sick. Would you know they left right away (always), or would you find out at a later time when someone told you? This is where the "timeout" factor comes into play that people have described.
    They say, "Practice makes perfect," yet they also say, "Nobody's perfect". I don't get it.

  10. #10
    SitePoint Zealot Beckwith's Avatar
    Join Date
    Mar 2004
    Location
    California
    Posts
    149
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ah you rule malikyte thanks for the explanation that really helped, I didn't realize that you could set a timeout factor in a page

  11. #11
    SitePoint Addict
    Join Date
    Oct 2004
    Location
    USA, Saratoga Springs, NY
    Posts
    296
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well, although PHP does have a "timeout" setting, it has nothing to do with this discussion. The timeout that I was referring to was the amount of time that you would set by default for a user's browsing time.

    Since no one knows exactly how long someone will visit your site (though you can make a rough estimate based on the type of visitors your site would normally receive), within these PHP/MySQL scripts that people are talking about, and because PHP cannot know when someone leaves, you have to make somewhat of a "hack" to "guestimate" if someone is still viewing your webpage.

    Since most people will not visit a single page on your website for more than 5 minutes, a lot of these scripts set a "viewing_timeout" sort of setting to individual IP addresses that are found viewing your pages.

    On each page load: first, we get the IP address (and/or username). We also need to get the "page accessed" time, usually in UNIX timestamp format (no need to understand what that means if you don't, it's basically just the current time all in seconds). We also use the "timeout" setting that we've identified as a focus point. Therefore, if anyone we've ALREADY recorded as visiting the website has not yet refreshed any pages (and thus had their IP and current time updated), they are removed from the list of current visitors after the designated time (i.e.: 5 mins).

    I didn't really think this point through, so it's somewhat of a poor description...if you don't understand it, I'll try and break it down further, in a better and more understandable way.
    They say, "Practice makes perfect," yet they also say, "Nobody's perfect". I don't get it.

  12. #12
    SitePoint Wizard swdev's Avatar
    Join Date
    Oct 2004
    Location
    UK
    Posts
    1,053
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Let me clarify what I was trying to say in my earlier post.

    to count the number of users online at a given point in time, takes 2 parts

    Part 1
    =====

    Each time a user surfs to a page, that user's ipaddress is saved in a table (say online), along with the time they access the page.
    If the ipaddress already exists in the table, the record is just updated, otherwise a new record is added.
    This script needs to be added to every page that is to be monitored.

    Part 2
    =====
    A script is run, via a scheuler (e.g. cron if on Unix) at predetermined times, say every 5 minutes, or every hour, whatever time period you decide.
    This script reads the online table (the same one that is updated in part 1) and deletes all those entries whose timestamp is older than the current time - your selected timeout period.

    What this will leave you with is a table (online) that contains information about those surfers who have recently visited you page - i.e. the number of online users.

    Becasue of how the web is designed (i.e. stateless) we will only ever get an appoximate value, but this is the best we can do.

    Hope this helps.

  13. #13
    SitePoint Addict
    Join Date
    Oct 2004
    Location
    USA, Saratoga Springs, NY
    Posts
    296
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    swdev, why use cron if you update the table on each page view anyway? You don't necessarily need this updated on intervals if it isn't to be used except by the visitors anyway. cron/task scheduler would just add unnecessary overhead.
    They say, "Practice makes perfect," yet they also say, "Nobody's perfect". I don't get it.

  14. #14
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,807
    Mentioned
    158 Post(s)
    Tagged
    3 Thread(s)
    Consider this.....

    User logs in, closes the browser window and goes home.
    Nobody visits your site for 24 hours. (so no pages are loaded)
    User comes back, opens the page = still logged in after 24 hours

    Using a cron job would remove any possibility of this as it runs constantly whether any pages are loaded or not..

    SpikeZ
    Mike Swiffin - Community Team Advisor
    Only a woman can read between the lines of a one word answer.....

  15. #15
    SitePoint Wizard swdev's Avatar
    Join Date
    Oct 2004
    Location
    UK
    Posts
    1,053
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks spikeZ for the cron reply. you have got my intentions perfectly

  16. #16
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,807
    Mentioned
    158 Post(s)
    Tagged
    3 Thread(s)
    Quote Originally Posted by swdev
    Thanks spikeZ for the cron reply. you have got my intentions perfectly
    It's strange, almost a physic connection between us ooooooooh

    SpikeZ
    Mike Swiffin - Community Team Advisor
    Only a woman can read between the lines of a one word answer.....

  17. #17
    SitePoint Addict
    Join Date
    Oct 2004
    Location
    USA, Saratoga Springs, NY
    Posts
    296
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)


    I should have been more specific. I probably would be doing an update before displaying any information to the page anyway. To be honest, I don't see doing it any other way. If you must first check to see if the IP already exists within the table and do an update if it does, why not also (within the same query) update all records that do not meet a certain criteria for the time.

    Adversely, with an insert, you would have to make 2 queries unless you are using MySQL v4+. (I think it's v4 that supports multi-queries per statement.)

    I suppose using cron or task scheduler would be more efficient considering most visitors viewing pages would be first-hits, as opposed to updates...or would it? I'm not entirely sure myself. I suppose it depends more on what your site is used for.

    [ edit ] - I just re-read SpikeZ's first reply. I misunderstood. Even so, with a slightly modified UPDATE, the same process could be achieved. The biggest question here is with efficiency, imho.
    They say, "Practice makes perfect," yet they also say, "Nobody's perfect". I don't get it.

  18. #18
    SitePoint Wizard swdev's Avatar
    Join Date
    Oct 2004
    Location
    UK
    Posts
    1,053
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You can use the REPLACE MySQL syntax to do either an INSERT or UPDATE in a single query.

    I think it is more efficeint to just display the data on the relevant pages (just a select count(*) type query), and let a scheduled job handle the removal of old entries (a delete ... where type query), otherwise you are deleting, updateing and selecting from the online table for every single page view.

  19. #19
    SitePoint Addict
    Join Date
    Oct 2004
    Location
    USA, Saratoga Springs, NY
    Posts
    296
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ...

    I can't argue that one.
    They say, "Practice makes perfect," yet they also say, "Nobody's perfect". I don't get it.


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
  •