Home>Article>Backend Development> How to modify cookie content in php?
How to modify cookie content in php: The cookie must be assigned a value before any other output is sent. The code is [$_COOKIE[$var] = $value;setcookie($var,$value,$time,$ path,$domain,$s);].
php method to modify cookie content:
Cookie is a variable sent by the server to the browser. Cookies are typically small text files that a server embeds on a user's computer.
This cookie is sent each time the computer requests a page through the browser.
The name of the cookie is specified as a variable with the same name. For example, if the cookie being sent is named "name", a variable named $user is automatically created containing the cookie's value.
The cookie must be assigned before any other output is sent.
If successful, the function returns true, otherwise it returns false.
Today when I was doing exercises, I encountered the problem that the cookie in PHP must be refreshed to take effect. It 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, there is no need to refresh, and the value of the cookie can be obtained directly. Yes, cookie parameter
Tip: In this code, these two sentences are effective for instant update of cookies:
$_COOKIE[$var] = $value; setcookie($var,$value,$time,$path,$domain,$s);
Related learning recommendations:PHP programming from entry to proficient
The above is the detailed content of How to modify cookie content in php?. For more information, please follow other related articles on the PHP Chinese website!