Search code examples
phpsession

PHP Sessions Not Extending Cookie Expiration on Each Request


Is session_start() supposed to extend the life of the session ID cookie by the session.gc_maxlifetime variable?

My session.gc_maxlifetime is 24 minutes, and each session is only living 24 minutes regardless of additional activity on the site. I get my session, refresh the page, and the expiration time does not change. This results in a logout after 24 minutes of login, no matter what. Is there something wrong with my configuration?


Solution

  • I think this post from user 'Glass Robot' will provide the solution you are looking for: https://stackoverflow.com/a/1236583/1487529

    Basically, when session_start() is called, there is a 1% probability (by default) that the garbage collector will be run. When the garbage collector is run it scans for and deletes expired sessions. However, when you are the only user accessing the page (which you probably are, during development) or there are very few users, the garbage collector will only run when you access a page. This happens AFTER session_start() is called, effectively resetting the timer. Instead of trying to work around this, just implement your own session_start() function which enforces the timeout. Try the function that the @Glass Robot posted, in the link I gave you above.