Howdy, all you young geniuses! The old geezer has a question -- not sure if it belongs here or in the "coding" forum:
I'm putting together a routine in a "My Account Info" page that shows a user the number of times he has logged into the application, how much time has elapsed in this session, and the total amount of time the user has spent in the application for all logins. This last one is the tricky part.
If the user does a normal logout after she finishes a session, then that time is recorded and used to calculate the total time. But what if she just exits a page without logging out, or the computer goes down?
Is there some way PHP can check when a session has been interrupted and do some housekeeping or cleanup without a signal coming from the browser? I've got some ideas, but I don't know if they're any good. I'd like to hear the thoughts of some with more talent in this field...
Logging Out vs Exiting Issues
Moderator: General Moderators
- llanitedave
- Forum Commoner
- Posts: 78
- Joined: Thu Jan 15, 2004 11:24 am
- Location: Las Vegas, NV.
-
penguinboy
- Forum Contributor
- Posts: 171
- Joined: Thu Nov 07, 2002 11:25 am
You could set a short session timeout.
After, 20 mins of inactivity logout the user.
Or you could create a hidden iframe the uses a meta refresh
every ... 5mins or so to keep the session alive.
But then you have the problem of 'what if the users doesn't close the page and they don't logout.
In my opinion the best thing to do is just rely on a session timeout.
Usually this type of logging of isn't necessary;
Instead its just a 'fun fact' or whatever... if you know what I mean.
If you absolutly have to keep a strict record though;
you should set a short session timeout.
After, 20 mins of inactivity logout the user.
Or you could create a hidden iframe the uses a meta refresh
every ... 5mins or so to keep the session alive.
But then you have the problem of 'what if the users doesn't close the page and they don't logout.
In my opinion the best thing to do is just rely on a session timeout.
Usually this type of logging of isn't necessary;
Instead its just a 'fun fact' or whatever... if you know what I mean.
If you absolutly have to keep a strict record though;
you should set a short session timeout.
- llanitedave
- Forum Commoner
- Posts: 78
- Joined: Thu Jan 15, 2004 11:24 am
- Location: Las Vegas, NV.
Thanks, penguinboy. The session timeout may be what I need. In the case of this application, displaying the total time is a "funfact", but I DO need to track the user's activity if they make any changes to the database. I may just use their last recorded activity -- or a combination of both.
Thanks again, you got me thinking...
Thanks again, you got me thinking...
-
ilovetoast
- Forum Contributor
- Posts: 142
- Joined: Thu Jan 15, 2004 7:34 pm
Session timeouts may/can meet your needs but are sort of an indirect means of getting at the info you're after. For a more direct method (albeit a more work intensive one) I would suggest you follow the idea in your last post - use their last recorded activity. Personally I don't like the refreshing iframe or its ugly cousins - no sense clogging your server with thousands and thousands of extra hits if you don't need to.
It's fairly easy to track user activity around an application without incurring significant overhead. You can use alternate apache log files, hacks of the php error log, a flat file of your choosing, or a database - just something that quickly marks each time a user takes an action. If you're already using a logging solution it may work well within that.
The start time is the time of the first action - login or arrival as it were. The end time is a little more effort, but not too much. It involves setting an expected inactivity/latency time. You could do this site wide by averaging time spent on pages using your logs, you could do it page by page with all users factored in, or you could even do it as detailed as average time spent on each page for this user only.
For example, your logs might show that the average time spent on a page is 1 minute. If a user doesn't log out correctly, figure their time by adding the 1 minute onto the time elapsed up to the last recorded activity. You can adjust the average as needed to accomodate slow users, more detailed pages, etc.
You can't push session timeouts low enough to attain accuracy without causing some inadvertant timeouts to occur. It is impossible. When you start getting near to an accurate point, more and more users will overstay and prematurely expire. A session timeout has implications beyond just the time online calculation. Implications that you may want to avoid.
It is a little more work, but if accuracy is what you're after this may be one way to go. After all, if the average time on a page is 1 minute (for example), then arbitrarily adding 20 minutes (or any other session timeout) to someone's time online is likely going to introduce major errors.
peace
It's fairly easy to track user activity around an application without incurring significant overhead. You can use alternate apache log files, hacks of the php error log, a flat file of your choosing, or a database - just something that quickly marks each time a user takes an action. If you're already using a logging solution it may work well within that.
The start time is the time of the first action - login or arrival as it were. The end time is a little more effort, but not too much. It involves setting an expected inactivity/latency time. You could do this site wide by averaging time spent on pages using your logs, you could do it page by page with all users factored in, or you could even do it as detailed as average time spent on each page for this user only.
For example, your logs might show that the average time spent on a page is 1 minute. If a user doesn't log out correctly, figure their time by adding the 1 minute onto the time elapsed up to the last recorded activity. You can adjust the average as needed to accomodate slow users, more detailed pages, etc.
You can't push session timeouts low enough to attain accuracy without causing some inadvertant timeouts to occur. It is impossible. When you start getting near to an accurate point, more and more users will overstay and prematurely expire. A session timeout has implications beyond just the time online calculation. Implications that you may want to avoid.
It is a little more work, but if accuracy is what you're after this may be one way to go. After all, if the average time on a page is 1 minute (for example), then arbitrarily adding 20 minutes (or any other session timeout) to someone's time online is likely going to introduce major errors.
peace