note 100672 added to function.session-set-cookie-params
| From: | php at rijkvanwel dot nl | Date: | Fri, 29 Oct 2010 09:32:13 +0000 |
| Subject: | note 100672 added to function.session-set-cookie-params | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-173590@lists.php.net to get a copy of this message | ||
No matter how your server's gc is configured, a user will still experience an expired session
when the cookie containing his session id is dropped.
The problem (that some people before me have already noticed) with this is, that the cookie's
timestamp is not updated on every new request. So while the visitor's navigating through your
site or application, his session file's timestamp is updated and your gc_lifetime is configured
properly so the file's not deleted by the server, but meanwhile the clock is ticking away on
his locally stored session cookie. When it reaches the time limit: boom, session gone. (Actually,
the session is still there on the server, the browser just stops sending the id with the requests,
so the server can't identify the visitor and starts a new session.)
One solution for this is to regenerate the cookie with each request (either by manually overwriting
the cookie with setcookie(), or by regenerating the session id with session_regenerate_id()).
However, this is needless overhead.
If it works for your application, a better and much simpler solution is to just set the session
cookie's lifetime to 0 (which will make it live until the browser is closed):
<?php
session_set_cookie_params(0);
session_start();
?>
You will only have to do this the first time your session is started (as this is the only time the
cookie is sent to the client).
----
Server IP: 213.249.64.162
Probable Submitter: 82.93.68.91
----
Manual Page -- http://www.php.net/manual/en/function.session-set-cookie-params.php
Edit -- https://master.php.net/note/edit/100672
Del: integrated -- https://master.php.net/note/delete/100672/integrated
Del: useless -- https://master.php.net/note/delete/100672/useless
Del: bad code -- https://master.php.net/note/delete/100672/bad+code
Del: spam -- https://master.php.net/note/delete/100672/spam
Del: non-english -- https://master.php.net/note/delete/100672/non-english
Del: in docs -- https://master.php.net/note/delete/100672/in+docs
Del: other reasons-- https://master.php.net/note/delete/100672
Reject -- https://master.php.net/note/reject/100672
Search -- https://master.php.net/manage/user-notes.php