note 78624 modified in function.setcookie by danbrown

From: Date: Sun, 17 May 2009 17:49:24 +0000
Subject: note 78624 modified in function.setcookie by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-155027@lists.php.net to get a copy of this message
Observed: session cookies were expiring even though the session was still active. (To test, set a cookie expiry of 5 seconds and keep hitting the page every second. The session will expire and create a new SESSID after 5 seconds despite the fact that you hit the page only a second ago.) Calling this function before starting the session fixed it. It copies the cookie contents back to itself while forcing an update to the expiry time in the cookie. <?php function FreshenSessionCookie($lifetimeSeconds, $cookieName = 'PHPSESSID') { if (isset($_COOKIE[$cookieName])) { $data = $_COOKIE[$cookieName]; $timeout = time()+$lifetimeSeconds; setcookie($cookieName, $data, $timeout); } } ?> --was-- Observed what I think is a bug: session cookies were expiring even though the session was still active. (To test, set a cookie expiry of 5 seconds and keep hitting the page every second. The session will expire and create a new SESSID after 5 seconds despite the fact that you hit the page only a second ago.) Calling this function before starting the session fixed it. It copies the cookie contents back to itself while forcing an update to the expiry time in the cookie. function FreshenSessionCookie($lifetimeSeconds, $cookieName = 'PHPSESSID') { if (isset($_COOKIE[$cookieName])) { $data = $_COOKIE[$cookieName]; $timeout = time()+$lifetimeSeconds; setcookie($cookieName, $data, $timeout); } } http://php.net/manual/en/function.setcookie.php

« previous php.notes (#155027) next »