4

I am thinking of implementing a small tracking system that will track users logon / logoff times and also track who is currently logged on. It all makes sense to me except for the tracking of when a user logs off. Its easy enough when a user clicks a "log off" button which calls a function that can update the database with the logoff time but what if the user just exits out of the browser. How can I create a function that will handle this event and I guess any other situations I have not thought of when a user exits a php application an informal way. Also, any links to online tutorials on this subject would be much appreciated.

  • 1
    "I have not thought of when a user exits a php application an informal way." - a browser crash – Shoban Nov 24 '09 at 11:58
  • Wouldn't it be easier to keep track of when they're logged in? – Brendan Long Nov 24 '09 at 12:04
  • I am tracking when they are logging on. But I would like the capability to see who is currently logged on and that means that I would need to also track when a user has a logged off to set the active flag to 'inactive' for example. It seems rather redundent to call a function on every action of the user to determine they are still active but I guess that is one way to do it –  Nov 24 '09 at 12:08
  • Well what I mean is that you can't call a function to check if the user is inactive, so it makes more sense to keep track of when they are active, and then just make your tracking software decide how to interpret it. – Brendan Long Nov 24 '09 at 16:54

3 Answers3

12

The simplest solution is probably just to mark a user as "active" each time he does an action on your site (ie, each time a page is generated on the server) -- you already know how to do that.

And if a user has been marked as "active" a long time (ie, more than 5 minutes, for instance ; maybe more depending on the kind of content you have on your site) ago, you can consider he is not active anymore -- maybe not even on your site anymore, actually.

Of course, you have a couple of minutes of delay before detecting a user is not there anymore...


A possible complementary solution would be to use some kind of Ajax request done once every one or two minutes : this way, you have a shorter delay before detecting a user is not here anymore...

But, with this solution : you consider a user as active if he has a browser window/tab opened on your site -- even if he is not really using it (I always have lots of tabs opened in my firefox, some on websites I don't actually use for hours / days !)

Pascal MARTIN
  • 395,085
  • 80
  • 655
  • 663
  • For determining if user is active on a tab: http://stackoverflow.com/questions/1777089/measuring-online-time-on-website/1777146#1777146 – vsr Nov 24 '09 at 12:33
3

you can use Javascript to call PHP function (via AJAX) on window unload event or just log user out after e.g. 30 minutes of inactivity automatically.

dusoft
  • 11,289
  • 5
  • 38
  • 44
2

You won't be able to track when the user closes the browser or your page.

What I have attempted to ask about tracking user closing page: AJAX - when user leaves page - good or bad practice/implementation?

Apparently it's not that good to send data on the before_unload event.

Well what you can do is to consider users being logged off after inactivity for certain amount of time.

Each time when the users goes to a page, you store the timestamp. A user is said to be offline if the timestamp is more than 5 minutes old for example. You can store the timestamp into database or session.

Alternatively if you wish for it to be more accurate, you can call AJAX-ly every time interval (say 30 seconds) back to your server to prolong your activity timestamp.

Community
  • 1
  • 1
mauris
  • 42,982
  • 15
  • 99
  • 131
  • Well then how does a system know who is currently logged on such as a forum. For instance I am a member of stackoverflow. If I do not store cookies etc and close my browser I am essentially logged out. I know that many forums have the ability to query who is currently logged on. It is not a safe assumption to just go by some time metric to say that after that amount of time I am logged out because I could be on this forum for hours or days or whatever. –  Nov 24 '09 at 12:05