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

PHP implements cookies to take effect immediately

藏色散人
Release: 2023-04-08 16:50:02
forward
2262 people have browsed it

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);
    }
}
Copy after login

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);
Copy after login

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!

Related labels:
php
source:segmentfault.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template