Page 1 of 1

Close the browser without signout

Posted: Wed Jan 27, 2010 7:51 am
by nithinkk
In my login system logout time is noted when user click the logout button. But if the user close the window it will not update the logout time in the database. How to know when user clicks close button with out sign-out....can any one suggest what to do ?????

My database will insert username and login time when the user enter and it also updates the signout time when the user click logout . But i dont know what to do when user closes with out signing out.....HOW TO UPDATE THE LOG OFF TIME in my database ???

Re: Close the browser without signout

Posted: Wed Jan 27, 2010 8:21 am
by requinix
You can't know for sure when that happens.

Closest you can get is using some JavaScript to make an AJAX call when the window closes, but no guarantees that it will work.

Re: Close the browser without signout

Posted: Wed Jan 27, 2010 8:56 am
by JNettles
Your best bet is probably to store your sessions in the database and run a script that periodically checks whether or not they've been active in the past 15 minutes (or however long). Then you can run cleanup operations.

Re: Close the browser without signout

Posted: Wed Jan 27, 2010 9:34 am
by Iblood
you can use AJAX to measure the logged in time instead, just loop your AJAX function for let's say every 1 sec. and store the time to your database,
Ex:
You have this column [ logout time ]
You have this php file <? store time() to [ logout time ] ?>
What will happen here is when your page is up and running, it will trigger your AJAX function and your php will store the current time to your database,
When the user closes your page or browsers, the AJAX looping will definitely stop and the last recorded time will be the logout time you need.

I hope this could help..
Thanks

Re: Close the browser without signout

Posted: Wed Jan 27, 2010 12:58 pm
by JNettles
Save the time to the database every 1 second? What happens the moment he has a multitude of users online, all saving their time every single second to the database? This is grossly inefficient and a good way to crash your server.

Re: Close the browser without signout

Posted: Wed Jan 27, 2010 4:32 pm
by Iblood
haven't thought of that :lol:

Anyway onunload event would be great for this, but as stated on the some posts I've read there are certain issues with this on other browser's , is that true JNettles or anyone :? , I haven't tried that yet.. will try it some time..

Re: Close the browser without signout

Posted: Wed Jan 27, 2010 4:45 pm
by VladSun
"On unload" won't work if multiple windows/tabs are opened. You'll receive a "logout" event even if the user is still browsing your site in another window/tab.
I'd go with the AJAX "still-logged-in" calls, though I'll update the DB on every 5 minutes.

Re: Close the browser without signout

Posted: Thu Jan 28, 2010 8:00 am
by JNettles
Some of this will depend on your architecture but if you're implementing an MVC style site you could log the time whenever a user requests a page without much difficulty. That's still a bit of a hit on your performance but it might be the best bet rather than trusting to Ajax calls or trying to guess when the window closes.

Re: Close the browser without signout

Posted: Thu Jan 28, 2010 3:29 pm
by VladSun
I tend to use a dedicated session-keep-alive AJAX call in a loop. I found this very useful for a heavy AJAX-featured pages, where a session time out can be a big problem. That's why I recommend the AJAX "user-still-logged-in" approach.

My session-keeper JS/PHP scripts also checks whether the user's role (ACL) has been changed (reloading is insisted), whether the client side software version has been changed (upgrades notifier), etc. So, it really doesn't matter for me whether it will update the user's last-login-time or not :) - just another feature ;)

Re: Close the browser without signout

Posted: Fri Jan 29, 2010 4:26 am
by nithinkk
Thankz guys for your suggestions its solved :-) what i did is....i update the logout time when a user click some links....so i will get some what exact time if user close the Browser