Page 1 of 1

How do detect if a session has expired.

Posted: Fri Aug 08, 2008 7:32 am
by impulse()
I have a login page and I have set the session to expire after 10 minutes with the following:

Code: Select all

ini_set("session.gc_maxlifetime", "600");
If a page is reloaded after that time I'm guessing the $_SESSION() array is empty. Is that correct? If so then I'll just checking if $_SESSION['username'] is set upon each page load.

Re: How do detect if a session has expired.

Posted: Fri Aug 08, 2008 7:35 am
by ghurtado
Thats correct, after the session expires, all the session variables should be empty.

Re: How do detect if a session has expired.

Posted: Fri Aug 08, 2008 7:37 am
by impulse()
Thank you.

Re: How do detect if a session has expired.

Posted: Sun Aug 10, 2008 2:37 pm
by impulse()
I have tested this and I don't seem to be able to get it to work. I have wrote

Code: Select all

ini_set("session.gc_maxlifetime", "600");
before starting the session but the $_SESSION array still contains data after the maxlifetime has expired.

Does anybody have any suggestions why?

Re: How do detect if a session has expired.

Posted: Mon Aug 11, 2008 8:38 am
by ghurtado
Did you also try changing this in the PHP.ini ? it is possible that your configuration prevents you from changing that value directly. Do you have a full script example?

Re: How do detect if a session has expired.

Posted: Mon Aug 11, 2008 8:53 am
by Bill H
I think this one is going to be a little tricky. Using ini_set() changes the setting for the duration of the execution of the script only, and allows it to return to the original setting when the script terminates. So you have to be certain that you set the lifetime before the session is created, and that you create the session while the same script is running. It would be easy to lose that particular sequence, either creating the session in a earlier script, or in a script subsequent to the one using ini_set().

Re: How do detect if a session has expired.

Posted: Mon Aug 11, 2008 9:15 am
by impulse()
I haven't changed it direct in php.ini.

My site works from an index page which then uses a switch statements to determine which page to include. So session_start is only used once and that's in the index page. If this is going to be a problem then I can use a less preffered way of inserting rows into a database for each IP upon a page request, and kill the session if the last page accessed was over the 600 second timeframe.

Your thoughts are welcome though.

Re: How do detect if a session has expired.

Posted: Mon Aug 11, 2008 12:42 pm
by ghurtado
Like Bill said, ini_set works only on the current page, so you may be able to avoid your problems by setting it in php.ini instead.