Making sessions expire

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
citizen_cain
Forum Newbie
Posts: 7
Joined: Fri Jun 13, 2003 1:38 pm

Making sessions expire

Post 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
SBukoski
Forum Contributor
Posts: 128
Joined: Wed May 21, 2003 10:39 pm
Location: Worcester, MA

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