note 98096 deleted from function.setcookie by googleguy
| From: | googleguy@php.net | Date: | Tue, 15 Jan 2013 05:12:11 +0000 |
| Subject: | note 98096 deleted from function.setcookie by googleguy | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-193068@lists.php.net to get a copy of this message | ||
Note Submitter: danjr33 AT [google]mail.com
----
I haven't seen any notes yet on this, so I wanted to give an example of setting a cookie and
having the value available BEFORE you reload the page.
First, a disclaimer: this is only a workaround and it does not verify that the cookie was properly
saved.
In some cases, it may be important to be able to access a cookie on the same page load. For example
for my website I was using $_COOKIE['language'] to store and check the current interface
language for the user. When this was changed I needed to use the new value on the SAME page load,
and I didn't want to add a conditional statement to the later code.
This function is exactly identical to setcookie() except that it also sets
$_COOKIE['cookiename'] to "cookievalue" and that will then be available on the
same page load.
<?php
function setcookielive($name, $value='', $expire=0, $path='',
$domain='', $secure=false, $httponly=false) {
//set a cookie as usual, but ALSO add it to $_COOKIE so the current page load has access
$_COOKIE[$name] = $value;
return setcookie($name,$value,$expire,$path,$domain,$secure,$httponly);
}
?>