Page 1 of 1

Making sessions expire

Posted: Fri Jun 13, 2003 2:21 pm
by citizen_cain
I have a question about sessions. I am fairly new to PHP and sessions especially, but I have learned enough to make them work and do what I need them to do.
The problem I would like to solve is as follows:

Every time a user logs on to the site I created, a session file is created in which I store some information. Now in order to kill that session a user has to log out, which if they do works fine. But if they do not log out and simply close the browser window, the session file still exists. My question is: Is there a way to make these sessions files "expire" so that they get automatically deleted after a certain period of time, or is it possible to detect when a user is closing the window without loggin out and write the session kill code when that happens?

Thanx

Posted: Fri Jun 13, 2003 2:47 pm
by SBukoski
I think the way a lot of people do it, or at least me, is they define how long they want sessions to last on their site. I have an initialization script that runs on each page. Any time a new session is created, it checks the table for old sessions. It will essentially delete from the table any sessions which have gone beyond what my sessions expiration time was defined as. Now, the session time is updated each time they view one of my pages so we know they are still viewing. When they close, this time will eventually pass, and will get deleted the next time someone logs in.