How do detect if a session has expired.

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
impulse()
Forum Regular
Posts: 748
Joined: Wed Aug 09, 2006 8:36 am
Location: Staffordshire, UK
Contact:

How do detect if a session has expired.

Post 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.
User avatar
ghurtado
Forum Contributor
Posts: 334
Joined: Wed Jul 23, 2008 12:19 pm

Re: How do detect if a session has expired.

Post by ghurtado »

Thats correct, after the session expires, all the session variables should be empty.
impulse()
Forum Regular
Posts: 748
Joined: Wed Aug 09, 2006 8:36 am
Location: Staffordshire, UK
Contact:

Re: How do detect if a session has expired.

Post by impulse() »

Thank you.
impulse()
Forum Regular
Posts: 748
Joined: Wed Aug 09, 2006 8:36 am
Location: Staffordshire, UK
Contact:

Re: How do detect if a session has expired.

Post 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?
User avatar
ghurtado
Forum Contributor
Posts: 334
Joined: Wed Jul 23, 2008 12:19 pm

Re: How do detect if a session has expired.

Post 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?
User avatar
Bill H
DevNet Resident
Posts: 1136
Joined: Sat Jun 01, 2002 10:16 am
Location: San Diego CA
Contact:

Re: How do detect if a session has expired.

Post 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().
impulse()
Forum Regular
Posts: 748
Joined: Wed Aug 09, 2006 8:36 am
Location: Staffordshire, UK
Contact:

Re: How do detect if a session has expired.

Post 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.
User avatar
ghurtado
Forum Contributor
Posts: 334
Joined: Wed Jul 23, 2008 12:19 pm

Re: How do detect if a session has expired.

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