Close the browser without signout

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
nithinkk
Forum Commoner
Posts: 55
Joined: Sat Nov 28, 2009 7:57 am

Close the browser without signout

Post 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 ???
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Close the browser without signout

Post 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.
User avatar
JNettles
Forum Contributor
Posts: 228
Joined: Mon Oct 05, 2009 4:09 pm

Re: Close the browser without signout

Post 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.
Iblood
Forum Newbie
Posts: 4
Joined: Tue Sep 15, 2009 10:25 am
Contact:

Re: Close the browser without signout

Post 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
User avatar
JNettles
Forum Contributor
Posts: 228
Joined: Mon Oct 05, 2009 4:09 pm

Re: Close the browser without signout

Post 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.
Iblood
Forum Newbie
Posts: 4
Joined: Tue Sep 15, 2009 10:25 am
Contact:

Re: Close the browser without signout

Post 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..
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: Close the browser without signout

Post 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.
There are 10 types of people in this world, those who understand binary and those who don't
User avatar
JNettles
Forum Contributor
Posts: 228
Joined: Mon Oct 05, 2009 4:09 pm

Re: Close the browser without signout

Post 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.
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: Close the browser without signout

Post 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 ;)
There are 10 types of people in this world, those who understand binary and those who don't
nithinkk
Forum Commoner
Posts: 55
Joined: Sat Nov 28, 2009 7:57 am

Re: Close the browser without signout

Post 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
Post Reply