I had problem with this too. I was thinking that each
session_set_cookie_params($sessionTime, '/', $domain);
session_start();
causes that expiration time for cookie PHPSESSID is extended. But really cookie PHPSESSID is set by session_start()
only first time in session when new session id is generated.
My goal was that session expiration time should regenerate each time a page was opened. I figured out that session can expire because of two reasons:
- Cookie PHPSESSID expires, and its expiration time isn't regenerated by
session_start()
, so session will always expire because of cookie with expiration time.
- No activity of user will cause that session will expire on server side. It is set by
ini_set('session.gc_maxlifetime', $sessionTime)
.
Solution in this case is when you won't set expiration time for cookie, but session.gc_maxlifetime
is still set:
function my_session_start($maxtime = 300)
{
ini_set('session.gc_maxlifetime', $maxtime);
session_set_cookie_params(0, '/', "." . $domain);
session_start();
}
Most important is 0
in session_set_cookie_params(0, '/', "." . $domain)
then cookie won't expire and there is no need to extend its expiration time. This cookie will be removed when browser is closed. Then we are limited only by time which expires on server side.
I had also problems with that I couldn't extend PHP session time by jQuery Ajax PINGs because of that I had set expiration time for PHPSESSID in cookie. 0
resolves all problems with not expected ends of sessions. Sorry for my English. Good luck.