Extending the Session in PHP with gc_maxlifetime not working
Question
I have the following code to extend my $_SESSION[]
variables. They expire after around 2-3 hours.
I tried to extend to 22h by changing the session.gc_maxlifetime
, but im still losing my session after a couple of hours.
//start sessions
ini_set('session.gc_maxlifetime', 60*60*22); // 22h - one day
//Output just to make sure config was changed.
echo ini_get("session.gc_maxlifetime");
session_start();
How do I increase the duration of my session?
Solution
Yes, it could be: session.cookie_lifetime which by default is 0, meaning terminate on closing of browser.
If you close your client and open it again session.gc_maxlifetime doesn't matter--it only applies to non-terminated sessions.
Or, you may have calles session start somewhere else earlier, with a smaller session.gc_maxlifetime. Often frameworks set this. Repeat calls to session start do nothing to alter the session.
OTHER TIPS
Try to change it in the php.ini ! As session lifetime is a filesystem thing, a per-script set up is always problematical.
There's a note on the manual page about this; if another script using the same directory to store session data doesn't have the maxlifetime set, it'll use the shorter value instead.