Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP Sessions Not Extending Cookie Expiration on Each Request

Tags:

php

session

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?

like image 900
Kirk Ouimet Avatar asked Sep 24 '10 23:09

Kirk Ouimet


People also ask

How do I extend the expiration date on cookies?

Extend the life of a cookie beyond the current browser session by setting an expiration date and saving the expiry date within the cookie. This can be done by setting the 'expires' attribute to a date and time.

How do I set cookies to expire at end of session?

To set a cookie so it expires at the end of the browsing session, simply OMIT the expiration parameter altogether. Save this answer.

What happen if cookie expires max age is session?

Cookies without an Expires or Max-Age attribute are treated as session cookies, which means they are removed once the browser is closed. Setting a value on either Expires or Max-Age makes them permanent cookies, since they will exist until they hit their expiry date.


2 Answers

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:

  1. Cookie PHPSESSID expires, and its expiration time isn't regenerated by session_start(), so session will always expire because of cookie with expiration time.
  2. 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.

like image 77
Ariel Bogdziewicz Avatar answered Nov 02 '22 22:11

Ariel Bogdziewicz


I've noticed this behavior in PHP and tried every configuration on PHP but no luck so far.

My sessions were dying on exact time from first session_start(), lookig at cookie lifetime, it was not renewing its expiry time.

My application already has an important client count, about 60 connections per second, so the GC was hit every 1.5s (i guess).

My solution for cookie time not extending was something like this (It may seem not to elegant, but worked for me).

function my_session_start($maxtime = 300){
    // $maxtime = 300 for 5 minutes
    session_start( [ 'gc_maxlifetime' => $maxtime ] );
    $_sess_name = session_name();
    $_sess_id = session_id();
    setcookie( $_sess_name, $_sess_id, time() + $maxtime, '/' );
}

It's my particular solution, as the question says "session ID cookie". May not be the optimal, but indeed it works for me!

like image 39
Frederic Yesid Peña Sánchez Avatar answered Nov 02 '22 22:11

Frederic Yesid Peña Sánchez