3

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?

Etienne Dupuis
  • 13,548
  • 6
  • 47
  • 58
  • 22h is one day? did i miss something? – mata May 16 '12 at 20:23
  • w/e changed it to 24h, 22h was to avoid relogging at 9am during work. it would log you out at around 7am, then you login at 9am. – Etienne Dupuis May 16 '12 at 20:25
  • @mata maybe because he wants to prevents automatic logouts when the user opens the browser every morning at the same time !? i think this is a very clever solution. – Sliq May 16 '12 at 20:27
  • 1
    Also, if you have multiple php.ini's for a site it may use the shortest session.gc_maxlifetime for all sites that share the same cache folder. since by default every session file is in the same place if a garbage collect is run it is run on all files in the cache. – Tyson of the Northwest Oct 30 '12 at 19:18

3 Answers3

2

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.

Ray
  • 40,256
  • 21
  • 101
  • 138
1

Try to change it in the php.ini ! As session lifetime is a filesystem thing, a per-script set up is always problematical.

Sliq
  • 15,937
  • 27
  • 110
  • 143
0

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.

andrewsi
  • 10,807
  • 132
  • 35
  • 51