note 72081 deleted from function.session-set-cookie-params by nlopess
| From: | nlopess@php.net | Date: | Sun, 21 Jan 2007 15:44:49 +0000 |
| Subject: | note 72081 deleted from function.session-set-cookie-params by nlopess | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-121992@lists.php.net to get a copy of this message | ||
Note Submitter: MaxTheDragon
----
This article states that one must define the lifetime of the cookie using
session_set_cookie_params(), before calling session_start(). This is correct.
However, suppose you would need to change the session cookie lifetime, during the session. You would
have to delete the session in order to pass the correct lifetime. If however, the session contains
important data, you might not want to delete the session.
This seems to be a problem, since the session already exists.
The following code:
<?php
setcookie(session_name(), $_COOKIE[session_name()], time()+10000, "/");
?>
allows you to change the cookie lifetime, during the session, and without the need to delete the
session. Basically it grabs the existing cookie, and just updates the lifetime, leaving the data
intact.
<?php
setcookie(session_name(), $_COOKIE[session_name()], 0, "/");
?>
Similarly, the above code allows you to turn the existing session, into one that is destroyed, when
the browser screen closes.
Please note that this is not a replacement of session_set_cookie_params! You must still call that
function before the session start to set a lifetime for the cookie. If somewhere after the
session_start(), you need to change the lifetime, use the code supplied above, and also make sure
that the new lifetime gets included in the next calls of
session_set_cookie_params(). This ensures that the cookie will work properly with the new lifetime,
even after page browsing/refreshes.
I have tested this, and it seems to work properly.