-
Notifications
You must be signed in to change notification settings - Fork 0
/
Cookie.php
108 lines (99 loc) · 2.19 KB
/
Cookie.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
<?php
/**
*
* -+-----------------------------------
* |PHP5 Framework - 2011
* |Web Site: www.iblue.cc
* |E-mail: mejinke@gmail.com
* |Date: 2012-06-13
* -+-----------------------------------
*
* @desc Cookie类
* @author jingke
*/
class XF_Cookie implements XF_Cookie_Interface
{
/**
* 默认会话命名空间
* @var string
*/
const NAMESPACE_DEFAULT = 'XF_MEMBER';
/**
* 当前会话命名空间
* @var string
*/
protected $_namespace;
/**
* 当前是否已上锁
* @var bool
*/
protected $_lock = FALSE;
/**
* 初始化session
* @param string $namespace 会话命名空间
*/
public function __construct($namespace = self::NAMESPACE_DEFAULT)
{
$namespace = (string) $namespace;
$this->_namespace = $namespace;
if (!isset($_COOKIE[$this->_namespace]))
setcookie($this->_namespace,'',time()-1);
}
/**
* session是否为空
* @return bool
*/
public function isEmpty()
{
if (!isset($_COOKIE[$this->_namespace]))
return true;
return XF_Functions::isEmpty($_COOKIE[$this->_namespace]);
}
/**
* 写入内容
* @param mixed $content
* @param int $expire 过期时间 默认为一周7天 单位:秒
* @param string $path 有效路径 默认为 "/"
* @param string $domain 域,默认为配置文件中的 “domain”
* @return XF_Cookie
*/
public function write($content, $expire = 604800, $path = '/', $domain = NULL )
{
if ($this->_lock == FALSE)
{
$this->_lock = TRUE;
if ($domain == NULL)
{
$domain = '.'.XF_Config::getInstance()->getDomain();
}
if ($content == null)
setcookie($this->_namespace, '1', time()-1, $path, $domain);
else
{
setcookie($this->_namespace, XF_Functions::authCode($content), time()+$expire, $path, $domain);
}
$this->_lock = FALSE;
}
return $this;
}
/**
* 读取内容
* @return mixed
*/
public function read()
{
if (isset($_COOKIE[$this->_namespace]))
return XF_Functions::authCode($_COOKIE[$this->_namespace], 'DECODE');
return null;
}
/**
* 是否存在指定的内容
* @param string $key 键
* @return bool
*/
public function hasContent($key)
{
$key = (string) $key;
return isset($_COOKIE[$this->_namespace][$key]);
}
}