Home >Backend Development >PHP Tutorial >PHP implements cookies to take effect immediately

PHP implements cookies to take effect immediately

藏色散人
藏色散人forward
2020-05-17 14:16:562282browse

PHP implements cookies to take effect immediately

Today when students were doing exercises, they encountered the problem that cookies in PHP must be refreshed to take effect. They can be solved by the following methods:

//   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, just directly You can get the value of the cookie. For details on the cookie parameters, please see the PHP manual.

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, for cookies Two assignments are made at once.

Recommended: "PHP Tutorial"

The above is the detailed content of PHP implements cookies to take effect immediately. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:segmentfault.com. If there is any infringement, please contact admin@php.cn delete