note 98096 added to function.setcookie
danjr33AT
| Newsgroups | php.notes |
|---|---|
| Message-ID | <[email protected]> |
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);
}
?>
----
Server IP: 69.147.83.197
Probable Submitter: 76.20.47.88
----
Manual Page -- http://www.php.net/manual/en/function.setcookie.php
Edit -- https://master.php.net/note/edit/98096
Del: integrated -- https://master.php.net/note/delete/98096/integrated
Del: useless -- https://master.php.net/note/delete/98096/useless
Del: bad code -- https://master.php.net/note/delete/98096/bad+code
Del: spam -- https://master.php.net/note/delete/98096/spam
Del: non-english -- https://master.php.net/note/delete/98096/non-english
Del: in docs -- https://master.php.net/note/delete/98096/in+docs
Del: other reasons-- https://master.php.net/note/delete/98096
Reject -- https://master.php.net/note/reject/98096
Search -- https://master.php.net/manage/user-notes.php