How to modify the cookie content in php: first open the content that needs to be modified; then modify and assign the cookie before any other output is sent. The code is [$_COOKIE[$var] = $value;setcookie($var ,$value,$time,$path】.
How to modify cookies in php:
The name of cookie
is specified as a variable with the same name. For example, if the cookie being sent is named "name", a variable named $user
will be automatically created containing the value of the cookie.
The cookie must be assigned before any other output is sent. If successful, the function returns true, otherwise it returns false.
Today I encountered PHP while doing an exercise The problem that the cookie in must be refreshed to take effect can be solved by the following method:
// php COOKIE设置函数立即生效,支持数组 function cookie($var, $value = '', $time = 0, $path = '', $domain = '', $s = false) { $_COOKIE[var] = $value; if (is_array($value)) { foreach ($value as $k => $v) { setcookie($var .'['.$k.']', $v, $time, $path, $domain, $s); } } else { setcookie($var,$value, $time, $path, $domain, $s); } }
In this way, you can get the cookie value directly without refreshing. Cookie parameter
Tips: In this code These two sentences are effective for instant update of cookies:
$_COOKIE[$var] = $value; setcookie($var,$value,$time,$path,$domain,$s);
That is to say, the cookie is assigned twice at once. The above are all the knowledge points introduced in this article.
Related Learning recommendations: PHP programming from entry to proficiency
The above is the detailed content of How to modify cookies in php?. For more information, please follow other related articles on the PHP Chinese website!