Page 1 of 1

logging a user out when the browser is closed

Posted: Mon Apr 21, 2003 2:16 pm
by preemi
hey, ive been searching forum after forum for a solution to logging a user out automatically after they close the browser window, in attempt to create an accurate list of users.

im using mysql, and i was planning on using the idea of comparing the current time with a future time...or something, but all ive seen on this site and devshed are vague references and i was wondering if someone who has done this could give me some insight on how to do this with specific examples (table definitions and the code to compare times would be greatly appreciated).

thanks a lot,
kevin

Posted: Mon Apr 21, 2003 3:31 pm
by AndrewBacca
im working on a simliar thing for my forum im creating.

When the user logsin username and current timestamp is added to a table on a database.
As the site is viewed users older than 360 seconds are deleted from the database.
The count can be inaccurate because if a person spends more than 360 seconds on a page they are deleted, but it's pretty full proof otherwise.

but away around that is to have it that, is when the user hits another page, it will check if the user is loged in the table if so then it will do nothing else it will add them again, then if the user goes idle for 360 seconds, when another user views a page, it checks the table for timestamps older then 360 seconds.

People being on the site are what keep it updated. A person has to view a new page to update the db etc.

AS for login them out automaticly, i came across this by accident if you create a session when they login, then when they close the broswer the session is destroy'd so they are signed out.


I hope thats wot you were after :)
if not sry!!

Andrew

Posted: Mon Apr 21, 2003 3:36 pm
by preemi
okay. after thinking about this for quite a while, i think i may have an idea.

when a user logs in, their username gets added to the logged_in table along with the current time, and the future value of current time+20min. at the same time, another section of code would compare all values in the logged_in table and delete all usernames that have the new current_time > the previous current_time+20min.

then, i would call a function at the top of each page that would update the current_time in the table and add the 20min to it. this way, as long as a person isn't on the same page for 20min, they would automatically get their session renewed for 20min each time they click on a different page.

please post ideas/opinions/other thoughts.

thanks a lot,
kevin

Posted: Mon Apr 21, 2003 5:05 pm
by Jade
I've tried something similar to that. Just be careful with that design beacause one of the problems i was running into with that was that people were getting logged off the site before they were supposed to be, or vice versa. I think i did fix the problem but I just wanted to give you a heads up in advance.

Jade

Posted: Tue Apr 22, 2003 3:12 pm
by SoToasty
I was thinking about this as I was pondering my own problem. Although this might not be solution for everyone, how about this. Create a frame at the top of the window make it one pixel tall. The HTML for the frame would be just a refresh tag set at about 30 seconds. Set the refresh tag to go to a PHP update page that updates the table stating user a is still on the page. Run a script once a minute and anyone with a timeout of > 45 secs gets logged off. I know a lot of people won't want to use frames, but it was just an idea.

Hope this helps.
Roger

Posted: Tue Apr 22, 2003 6:00 pm
by ckuipers
SoToasty wrote:I was thinking about this as I was pondering my own problem. Although this might not be solution for everyone, how about this. Create a frame at the top of the window make it one pixel tall. The HTML for the frame would be just a refresh tag set at about 30 seconds. Set the refresh tag to go to a PHP update page that updates the table stating user a is still on the page. Run a script once a minute and anyone with a timeout of > 45 secs gets logged off. I know a lot of people won't want to use frames, but it was just an idea.

Hope this helps.
Roger
You can also create a 'Flash' timer to get the same effect. Like that you won't have to use the frames...