phpcookie类

时间:2022-11-25 19:04:00

1.需求

写一个cookie类

2.例子

<?php

class cookie{
private $COOKIE_PREFIX = "";
private $COOKIE_EXPIRE = "";
private $COOKIE_PATH = "";
private $COOKIE_DOMAIN = ""; public function __construct()
{
//这里要加载config并赋值cookie的相关参数
} public function set_prefix($prefix='')
{
$this->COOKIE_PREFIX=$prefix;
} public function get_prefix()
{
return $this->COOKIE_PREFIX;
} public function is_set_cookies($name)
{
return isset($_COOKIE[$this->COOKIE_PREFIX . $name]);
} public function get_cookie($name)
{
//正式环境下,这里要解密cookie
$value = $_COOKIE[$this->COOKIE_PREFIX . $name];
return $value;
} public function set_cookie($name, $value, $expire = "", $PATH = "", $domain = "", $prefix = "")
{
if (empty($expire))
{
$expire = $this->COOKIE_EXPIRE;
}
else
{
$expire = time()+$expire;
}
if (empty($path)) {
$path = $this->COOKIE_PATH;
} if (empty($domain)) {
$domain = $this->COOKIE_DOMAIN;
} if (empty($prefix)) {
$prefix = $this->COOKIE_PREFIX;
}
//value要加密
setcookie($prefix . $name, $value, $expire, $path, $domain, NULL, true);
$_COOKIE[$prefix . $name] = $value;
} public function delete_cookie($name)
{
setcookie($name,'',-1);
$_COOKIE[$this->COOKIE_PREFIX . $name]=null;
unset($_COOKIE[$this->COOKIE_PREFIX . $name]);
}
public function clear_all_cookie()
{
$_COOKIE =array();
unset($_COOKIE);
}
}

3.待完善部分

cookie的设置和读取要加密和解密的过程。

4.总结