forked from typecho/typecho
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCookie.php
124 lines (113 loc) · 2.53 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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
<?php
/**
* cookie支持
*
* @category typecho
* @package Cookie
* @copyright Copyright (c) 2008 Typecho team (http://www.typecho.org)
* @license GNU General Public License 2.0
* @version $Id$
*/
/**
* cookie支持
*
* @author qining
* @category typecho
* @package Cookie
*/
class Typecho_Cookie
{
/**
* 前缀
*
* @var string
* @access private
*/
private static $_prefix = '';
/**
* 路径
*
* @var string
* @access private
*/
private static $_path = '/';
/**
* 设置前缀
*
* @param string $url
* @access public
* @return void
*/
public static function setPrefix($url)
{
self::$_prefix = md5($url);
$parsed = parse_url($url);
/** 在路径后面强制加上斜杠 */
self::$_path = empty($parsed['path']) ? '/' : Typecho_Common::url(NULL, $parsed['path']);
}
/**
* 获取前缀
*
* @access public
* @return string
*/
public static function getPrefix()
{
return self::$_prefix;
}
/**
* 获取目录
*
* @access public
* @return string
*/
public static function getPath()
{
return self::$_path;
}
/**
* 获取指定的COOKIE值
*
* @access public
* @param string $key 指定的参数
* @param string $default 默认的参数
* @return mixed
*/
public static function get($key, $default = NULL)
{
$key = self::$_prefix . $key;
$value = isset($_COOKIE[$key]) ? $_COOKIE[$key] : (isset($_POST[$key]) ? $_POST[$key] : $default);
return is_array($value) ? $default : $value;
}
/**
* 设置指定的COOKIE值
*
* @access public
* @param string $key 指定的参数
* @param mixed $value 设置的值
* @param integer $expire 过期时间,默认为0,表示随会话时间结束
* @return void
*/
public static function set($key, $value, $expire = 0)
{
$key = self::$_prefix . $key;
setrawcookie($key, rawurlencode($value), $expire, self::$_path);
$_COOKIE[$key] = $value;
}
/**
* 删除指定的COOKIE值
*
* @access public
* @param string $key 指定的参数
* @return void
*/
public static function delete($key)
{
$key = self::$_prefix . $key;
if (!isset($_COOKIE[$key])) {
return;
}
setcookie($key, '', time() - 2592000, self::$_path);
unset($_COOKIE[$key]);
}
}