tags:

views:

17

answers:

1

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?

+1  A: 

I think this post will provide the solution you are looking for: http://stackoverflow.com/questions/1236374/session-timeouts-in-php-best-practices

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.

SimpleCoder
So PHP doesn't automatically update the cookie on session_start()? I have to do that manually?
Kirk
It does update the expiration timer, but this timer is reset when you access another page. Try the function @Glass Robot posted (see my post).
SimpleCoder
Is it a bad idea to just regenerate the session ID on every request?
Kirk
This is actually a good idea but it won't help with this issue. When you regenerate the session ID, PHP automatically moves the session data into the new session. However, this will probably still reset the timer. I would recommend adding a block of code to each page that tracks the session expiration and manually closes the session if the session has expired.
SimpleCoder