Page 1 of 1
How to logout when a tab in browser is closed?
Posted: Fri Feb 08, 2008 11:00 pm
by Festy
Hello guys,
I'm working on a professional social networking community site and I want to achieve the following tasks in an efficient manner :
1. If a user is logged in, and he closes his tab in the browser and reopens it, he should be logged out when he types the url of the site.
2. If a user is logged in, and he closes his browser, he should be logged out when he types the url of the site.
I know second one is not a problem, but still I'd like to know how other advanced/expert php programmer do this. Can anyone help?
Re: How to logout when a tab in browser is closed?
Posted: Fri Feb 08, 2008 11:11 pm
by John Cartwright
Closing tabs is not the same as closing a window. Tabs are a bit tricky, since you have multiple tabs in the window running the same session, so if you close a tab then reopen it your still on the same session. This does not apply to windows since they are exclusive to each other.
Hopefully someone has a suggestion.. unfortunately thats all I can help.
Re: How to logout when a tab in browser is closed?
Posted: Fri Feb 08, 2008 11:50 pm
by thasmin
I doubt this could be done. The server isn't notified when a tab closes, so it doesn't know when to end the session. Closing the browser doesn't end the session either technically; it identifies itself differently and so a new session is opened.
Re: How to logout when a tab in browser is closed?
Posted: Sat Feb 09, 2008 1:18 am
by Kieran Huggins
This is NOT a good solution, but you could have very short sessions (2 min?) and use an ajax call every 20 sec or so to keep them alive.
There is definitely a smell to your design if you need a hack like this to make it work though, I recommend rethinking your approach. What are you trying to accomplish, generally?
Re: How to logout when a tab in browser is closed?
Posted: Sat Feb 09, 2008 2:16 am
by Festy
Kieran Huggins wrote:This is NOT a good solution, but you could have very short sessions (2 min?) and use an ajax call every 20 sec or so to keep them alive.
There is definitely a smell to your design if you need a hack like this to make it work though, I recommend rethinking your approach. What are you trying to accomplish, generally?
well our client want us to achieve this and I have no idea why. because I reckon there's no sense in getting logged out when you close a tab in your browser.
Re: How to logout when a tab in browser is closed?
Posted: Sat Feb 09, 2008 2:42 pm
by dayyanb
onunload
Just put that in the body tag. It obviously wont work when javascript is off. You could require users to turn on javascript or give them a warning that they wont be logged off.
Re: How to logout when a tab in browser is closed?
Posted: Sat Feb 09, 2008 4:36 pm
by califdon
Festy wrote:well our client want us to achieve this and I have no idea why. because I reckon there's no sense in getting logged out when you close a tab in your browser.
If your client wanted you to start the user's coffee maker when they closed their browser, would you agree to try to do it? As a web developer, part of your responsibility is to guide your client in their attempt to achieve some business goal. Especially when they ask for something that you don't understand the reasons for.
Re: How to logout when a tab in browser is closed?
Posted: Sun Feb 10, 2008 9:54 pm
by Festy
califdon wrote:Festy wrote:well our client want us to achieve this and I have no idea why. because I reckon there's no sense in getting logged out when you close a tab in your browser.
If your client wanted you to start the user's coffee maker when they closed their browser, would you agree to try to do it? As a web developer, part of your responsibility is to guide your client in their attempt to achieve some business goal. Especially when they ask for something that you don't understand the reasons for.
Unfortunately I'm the team member and not the leader so I can't communicate directly with my client, only my leader can. I've talked to my leader btw, but he tells us to try it. The agony of working under someone else.

Re: How to logout when a tab in browser is closed?
Posted: Sun Feb 10, 2008 10:05 pm
by Benjamin
Tell him to try turning some lead into gold

Re: How to logout when a tab in browser is closed?
Posted: Mon Feb 11, 2008 4:44 am
by Festy
Apparently, I tried the to achieve the above 2 tasks using 'onunload' event, but the problem with 'onunload' is, when I refresh a page, it gets called, hence the user is logged out of the system (which I don't want). Can anyone suggest me something here?
Re: How to logout when a tab in browser is closed?
Posted: Mon Feb 11, 2008 7:15 am
by Inkyskin
I don't think your going to find an easy way of doing this - like the others said, go back to the client and explain that they are asking for something that isn't feasable.
Re: How to logout when a tab in browser is closed?
Posted: Tue Feb 12, 2008 5:26 am
by Festy
Ok forget the 2nd issue (tab issue). and let's just concentrate on 1st one - when a user closes his browser or browser get closed unexpectedly. Now I'm having the following problem -
In my site, i want to restrict multiple logins with same username and password at a same time from different system.
To avoid this , in backend i.e. mysql database , i created a table which has login (boolean) field. If a user logged in , that login flag is set to true. If any other user tries to log in with same user name and password , i'm restricting that login . (Since that user is already logged in) . If logout link is clicked , i'm calling a php page , there the login flag is reset and session variables are destroyed. So there would not be any problem. Incase , if the browser is closed using X button in the top right corner or get closed unexpectedly, these login flag resetting process would not take place. Then how that user can login again ? when i should clear this flag?
Re: How to logout when a tab in browser is closed?
Posted: Tue Feb 12, 2008 5:46 am
by Inkyskin
If you refresh that table with every click, you'll also get what time they were last active. You could run a cron job to wipe any that were not active over say, 20 - 30 minutes ago, therefore still logging them out. Run that cron job every 10 minutes and your set to go.
Re: How to logout when a tab in browser is closed?
Posted: Tue Feb 12, 2008 9:14 am
by superdezign
Festy wrote:In my site, i want to restrict multiple logins with same username and password at a same time from different system.
Handle your sessions via the database and you'll have full control over it.
Code: Select all
create table `sessions` (
`user_id` int not null default 0,
`session_id` char(32),
`session_data` text,
`expires` datetime,
primary key(`session_id`));
Then, create a session handler that handle sessions in your database rather than your files by making use of
session_set_save_handler(). You would make the garbage collector get rid of expired sessions (and you could even call it on every page load if you wanted to), and you could overwrite sessions with the same user_id so that when a user logs in elsewhere, their old session no longer exists. You could also simply give them an error stating that they are already logged in, but that would force them to wait until the expiration period.
Re: How to logout when a tab in browser is closed?
Posted: Tue Feb 12, 2008 9:27 am
by liljester
i would use a timestamp instead of a boolean field. you wouldnt need a cron job in your database, just check that timestamp and see if the session has expired during the login process.
let me elaborate:
ill assume a session time of 10 minutes.
when a user tries to login, check thier last active timestamp, if its more than 10 minutes, allow them to login. If its less than 10 minutes, refuse the login. this system will require you to update the timestamp on every page load, wich is not a big deal. This also functions as an "auto log out", even if thier php session is still alive (but older than 10 minutes), they will have to log back in because the timestamp has expired.