Home > php教程 > php手册 > body text

PHP COOKIE及时生效的方法介绍

WBOY
Release: 2016-06-06 20:24:51
Original
1253 people have browsed it

本篇文章主要是对PHP中COOKIE及时生效的方法进行了介绍,需要的朋友可以过来参考下,希望对大家有所帮助

通常,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 Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template