当前位置:Gxlcms > PHP教程 > phpcookie类(用到了命名空间)

phpcookie类(用到了命名空间)

时间:2021-07-01 10:21:17 帮助过:16人阅读

分享一个php cookie操作类,用到了php中的命名空间,这个比较新颖,有需要的朋友参考下吧。

一个php cookie操作类,实现了基本的操作功能: 创建cookie、设置cookie的过期时间、注销cookie等。

代码:

name = (string) $name)){
if(!is_null($value)){
$this->value = (string) $value;
$this->expire = $expire;
$this->path = $path;
$this->domain = $domain;
$this->secure = $secure;
$this->httponly = $httponly;
} else {
$this->value = $this->exists() ? $_COOKIE[$this->name] : '';
}
} else {
throw new Exception("invalid cookie name");
}
}

/**
 * 检测cookie是否存在
 * @return boolean
 */
public function exists(){
return isset($_COOKIE[$this->name]);
}

/**
 * 通过setcookie设置cookie信息
 */
public function save(){
return setcookie($this->name, $this->value, $this->expire, $this->path, $this->domain, $this->secure, $this->httponly);
}

/**
* 注销cookie
*/
public function delete(){
return setcookie($this->name, "", time() - 3600);
}
}

}
?>

人气教程排行