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

PHP COOKIE及时生效的方法介绍_php技巧

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-05-17 08:49:39
Original
1718 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
Latest Issues
php data acquisition?
From 1970-01-01 08:00:00
0
0
0
PHP extension intl
From 1970-01-01 08:00:00
0
0
0
How to learn php well
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template