Home > Backend Development > PHP Tutorial > PHP COOKIE及时生效的方法介绍_PHP

PHP COOKIE及时生效的方法介绍_PHP

WBOY
Release: 2016-06-01 11:55:54
Original
837 people have browsed it

通常,php里要浏览器刷一下才能出现cookie,怎么才能让cookie及时生效呢,下面分享一个让cookie及时生效的一个方法,很实用,代码如下:
复制代码 代码如下:
/**
 * 设置cookie
 * @param string $name 键名
 * @param mixed $value 值
 * @param int $expire 过期时间,默认是一天
 */
public final function setCookie($name, $value, $expire = null){
    //cookie值为空,退出
    if(empty($value)) return;
    //过期时间
    if(empty($expire)) $expire = time() + 86400;
    $_COOKIE[$name] = $value;
    //判断value是否是数组
    if(is_array($value)){
        foreach ($value as $k => $v){
            if(empty($v)) continue;
            setcookie($name . "[$k]", $v, $expire);
        }
    }else{
        setcookie($name, $value, $expire);
    }
}

Related labels:
source:php.cn
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