User closes website without logging off

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
TorMike
Forum Newbie
Posts: 17
Joined: Thu Feb 25, 2010 9:14 am

User closes website without logging off

Post by TorMike »

My current website requires a user to logon. When they logon a field in a MySQL database (usr_logon) is timestamped. When they log off, a second field (usr_logoff) is timestamped.

I use this information to display the number of user on the site. However, if a user simple closes the site by closing their browser, or closing the tab, the field usr_logoff is never updated. As far has the base is concerned, when the next user logons, the number displayed will not be correct, since the usr_logoff field has not been timestamped.

How do I update the database when a user leaves the website by closing the tab or closing the browser thereby accurately displaying the true number of user logged on?

Thanks in advance for your solutions.
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: User closes website without logging off

Post by McInfo »

Track the time of the last request and assume that the user has left the site if a certain amount of time has passed since the last request.
TorMike
Forum Newbie
Posts: 17
Joined: Thu Feb 25, 2010 9:14 am

Re: User closes website without logging off

Post by TorMike »

Using a daemon??
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: User closes website without logging off

Post by McInfo »

No. Just as you record the time when a user makes a login request, record the time for other requests while the user is logged in. You would need a database field named something like "usr_last_request_at" that would be updated on each request. When determining the number of users on the site, count the number of records in the database where the difference between the current time and the last request time is greater less than some prescribed duration (maybe five minutes).
Last edited by McInfo on Wed Jan 12, 2011 3:13 am, edited 1 time in total.
kalpesh.mahida
Forum Commoner
Posts: 36
Joined: Wed Oct 06, 2010 7:09 am

Re: User closes website without logging off

Post by kalpesh.mahida »

or if you want to update your database flag while user leaving the page u can do so using onbeforeunload event as bellow. but you should not be rely on. whatever suggested by mcinfo seems logical and after some time lap of the login you can consider a person logged off. You can do one thing take time lap = session time out.

Code: Select all

<script type="text/javascript">
window.onbeforeunload = function()
{
    alert('call');
    // u r logic to update database flag
    return;
}
</script>
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: User closes website without logging off

Post by VladSun »

Another way (applied globally and in a single point) is to write a custom session handler and just record the time on session destroy:

http://www.daniweb.com/code/snippet216305.html
There are 10 types of people in this world, those who understand binary and those who don't
TorMike
Forum Newbie
Posts: 17
Joined: Thu Feb 25, 2010 9:14 am

Re: User closes website without logging off

Post by TorMike »

Wow!

Thanks all.

I added a field to my D/B (usr_activity) and added a small piece of code in the footer.php (on every page call) that simply updates the field with current date.

In be background I have a daemon running that checks to see if there has been a change in the field in the last 15 minutes (that's what my client wanted) and, if not activity, the user is logged off the site.

So combining the two source codes, I accomplished what was required.

Problem solved, thanks to your suggestions.
Post Reply