Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    Jun 2013
    Location
    Serbia
    Posts
    29
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Who's online not working

    I applied today User online script, submitted in a tutorial at: http://www.phpeasystep.com/phptu/9.html

    At first It seemed like it does all I expect it to do (just one thing actually), but it does not. It behaves like a regular hit counter. And I already have a hit counter.

    I'm moderating a low traffic Web site, and this "33 visitors online" is really poking my eyes for the last few hours. This can't be right.

    How do I modify this script into a reliable tracking code? I reckon data should be purged from the table every now and then, but I'm really a school example of a PHP newbie with a lot to learn. Any suggestions how to solve this tracking reliability problem?

    Thanks in advance?

  • #2
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,091
    Thanks
    51
    Thanked 506 Times in 493 Posts
    Well there are better and more efficient ways of doing what that code does but it should be doing what it says on the tin.

    The question is however what is your database showing? Do you have any duplicate records?

    I (years ago) had a similar script and found that instead of updating existing records it was duplicating them and updating the time. The key here is that you need to look at your database table in phpmyadmin and see if you have duplicates too. If you do then thats your problem.
    My helpful sig is on vacation trying to loose some weight. It got a bit fat and caused a few problems but it will be back at some point!

  • #3
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,979
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    As mentioned, duplicate records are possible due to the structure of the table. Although it *should* be finding that as well.
    Personally, I wouldn't bother with the select -> insert / update. I'd simply use an INSERT ON DUPLICATE KEY UPDATE syntax, which will require that the sessionid is specified as a primary key (or at minimum a unique key).

    I'd be looking towards the configurations for sessions. The defaults require that cookies be enabled, but its possible that a user does not have cookies enabled and has lost track of their session (transid is also not enabled by default). So I'd also suggest the first thing to look for is verify either:
    1. Duplicates
    2. All unique.
    If all unique, you're good to go. They may be the same *user* within them, but if the user doesn't provide a method of tracking them using cookies or you providing the sessionid in the querystring through manual placement (only required with header redirects), or through trans sid, than there is no way to track that user.

    Sessions do also have ways to manipulate them in an. . . easier(?) fashion. I use (?) there since its more work, but less maintenance to use the session handlers. PHP 5.4 has a class and interface for the SessionHandler[Interface] which can be implemented / overridden, but prior to that you can use individual function registers. These let you intercept the calls to each step in sequence for the session handling. You can choose what to do with them; you can intercept them and store in a single file for all sessions, or in a database, or use a combination of both or whatever you want to do.
    BTW, for your purposes it may be simpler to change the save path on the sessions to a local only directory, then iterate the directory and check the filemtime() on each of the session files. If its within the past 10 minutes, increment a counter as "online". This won't let you identify whom the user is (if you have a login system for example), but there are methods to do that as well, albeit taking a lot of work since there are three different serialization methods, and no method to extract them to a datastructure (only to the session superglobal from what I can tell). Somewhere down the line I evaluated the serialized structures and wrote handlers for each of them, but I haven't a clue what I did with that code (I don't typically use the sessionhandlers, but with the 5.4 interfaces I will move towards that with new versions). Fortunately, they can be changed in which case although it takes the most space, the wddx handlers themselves have a method of unserialize and serialize built in.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 


  •  

    Tags for this Thread

    Posting Permissions

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