9

I have a session manager page: sessmgr.php. This page is supposed to validate the user log-in information, set the cookies and keep the session variables alive by updating the cookies expiration time and regenerating the session id by an XML HTTP request at regular intervals till the user logs out. I am able to update the session id but not extend the cookie expiration time.

How to update the cookie expiration time here ??

Sᴀᴍ Onᴇᴌᴀ
  • 8,218
  • 8
  • 36
  • 58
Abhishek
  • 93
  • 1
  • 2
  • 5
  • If you set an existing cookie with a new value it updates the cookie instead of overwriting it so just set the expire date of all cookies every time – Anthony Dec 02 '13 at 11:28

3 Answers3

19

You should be able to update the expiration time with setcookie like:

setcookie("Cookiename", $value, NewExpirationTime)

Check if the Cookiename is exactly the same so that the old Cookie will be overwritten.

Sᴀᴍ Onᴇᴌᴀ
  • 8,218
  • 8
  • 36
  • 58
Maarkoize
  • 2,601
  • 2
  • 16
  • 34
  • i found the mistake that was not letting the cookies to be updated. The cookie value i was trying to write was getting null just before writing the cookie as i was trying to use $_GLOBALS instead of $_SESSION array. – Abhishek Dec 06 '13 at 10:05
  • @MarcelBalzer, Do you know Cookie will be not work from 2038? This is right? Because i have checked this link. https://stackoverflow.com/questions/3290424/set-a-cookie-to-never-expire – Bhavin Thummar Oct 26 '18 at 14:11
  • @MarcelBalzer well for now, yes, we're capped at 2038, but I think by the time we get to 2035 (if not sooner) we'll have implemented either a better system or converted to 64 bit so it's not really a concern – A Friend Nov 01 '18 at 20:56
0

just rewrite the cookie with the new time.

-2
if (isset($_SESSION['LAST_ACTIVITY']) && (time() - $_SESSION['LAST_ACTIVITY'] > SESSION_TIMEOUT)) { // SESSION_TIMEOUT is the amount of time you want the session to be.
    session_unset();
    session_destroy();
    header('Location: login.php');
}
$_SESSION['LAST_ACTIVITY'] = time();

Add this to your page. The if-statement checks if the session time has expired and redirects the user to loginpage.php. Otherwise it just updates the session time

Of course you change things to a cookie syntax. Missed that I use sessions and you wanted a cookie.

Skjaar
  • 89
  • 1
  • 10