1
<?php
2
/**
3
* Provides a consistent cookie API, HTTPOnly compatibility with older PHP versions and default parameters
4
*
5
* @author Zhou Yuan <yuanzhou19@gmail.com>
6
* @link http://www.infopotato.com/
7
* @copyright Copyright © 2009-2011 Zhou Yuan
8
* @license http://www.opensource.org/licenses/mit-license.php MIT Licence
9
* @link based on http://flourishlib.com/fCookie
10
*/
11
class Cookie {
12
/**
13
* The default domain to set for cookies
14
*
15
* @var string
16
*/
17
private static $_default_domain = NULL;
18
19
/**
20
* The default expiration date to set for cookies
21
*
22
* @var string|integer
23
*/
24
private static $_default_expires = NULL;
25
26
/**
27
* If cookies should default to being http-only
28
* Set the HTTPOnly flag on your session cookie and any custom cookies to prevent XSS
29
*
30
* @var boolean
31
*/
32
private static $_default_httponly = TRUE;
33
34
/**
35
* The default path to set for cookies
36
*
37
* @var string
38
*/
39
private static $_default_path = NULL;
40
41
/**
42
* If cookies should default to being secure-only
43
*
44
* @var boolean
45
*/
46
private static $_default_secure = FALSE;
47
48
/**
49
* Forces use as a static class
50
*
51
* @return Cookie
52
*/
53
private function __construct() { }
54
55
/**
56
* Deletes a cookie - uses default parameters set by the other set methods of this class
57
*
58
* @param string $name The cookie name to delete
59
* @param string $path The path of the cookie to delete
60
* @param string $domain The domain of the cookie to delete
61
* @param boolean $secure If the cookie is a secure-only cookie
62
* @return void
63
*/
64
public static function delete($name, $path = NULL, $domain = NULL, $secure = NULL) {
65
self::set($name, '', time() - 86400, $path, $domain, $secure);
66
}
67
68
69
/**
70
* Gets a cookie value from `$_COOKIE`, while allowing a default value to be provided
71
*
72
* @param string $name The name of the cookie to retrieve
73
* @param mixed $default_value If there is no cookie with the name provided, return this value instead
74
* @return mixed The value
75
*/
76
public static function get($name, $default_value = NULL) {
77
if (isset($_COOKIE[$name])) {
78
$value = UTF8::clean($_COOKIE[$name]);
79
if (get_magic_quotes_gpc()) {
80
$value = stripslashes($value);
81
}
82
return $value;
83
}
84
return $default_value;
85
}
86
87
88
/**
89
* Resets the configuration of the class
90
*
91
* @internal
92
*
93
* @return void
94
*/
95
public static function reset() {
96
self::$_default_domain = NULL;
97
self::$_default_expires = NULL;
98
self::$_default_httponly = FALSE;
99
self::$_default_path = NULL;
100
self::$_default_secure = FALSE;
101
}
102
103
104
/**
105
* Sets a cookie to be sent back to the browser - uses default parameters set by the other set methods of this class
106
*
107
* The following methods allow for setting default parameters for this method:
108
*
109
* - ::set_default_expires(): Sets the default for the `$expires` parameter
110
* - ::set_default_path(): Sets the default for the `$path` parameter
111
* - ::set_default_domain(): Sets the default for the `$domain` parameter
112
* - ::set_default_secure(): Sets the default for the `$secure` parameter
113
* - ::set_default_httponly(): Sets the default for the `$httponly` parameter
114
*
115
* @param string $name The name of the cookie to set
116
* @param mixed $value The value of the cookie to set
117
* @param string|integer $expires A relative string to be interpreted by [http://php.net/strtotime strtotime()] or an integer unix timestamp
118
* @param string $path The path this cookie applies to
119
* @param string $domain The domain this cookie applies to
120
* @param boolean $secure If the cookie should only be transmitted over a secure connection
121
* @param boolean $httponly If the cookie should only be readable by HTTP connection, not javascript
122
* @return void
123
*/
124
public static function set($name, $value, $expires = NULL, $path = NULL, $domain = NULL, $secure = NULL, $httponly = NULL) {
125
if ($expires === NULL && self::$_default_expires !== NULL) {
126
$expires = self::$_default_expires;
127
}
128
129
if ($path === NULL && self::$_default_path !== NULL) {
130
$path = self::$_default_path;
131
}
132
133
if ($domain === NULL && self::$_default_domain !== NULL) {
134
$domain = self::$_default_domain;
135
}
136
137
if ($secure === NULL && self::$_default_secure !== NULL) {
138
$secure = self::$_default_secure;
139
}
140
141
if ($httponly === NULL && self::$_default_httponly !== NULL) {
142
$httponly = self::$_default_httponly;
143
}
144
145
if ($expires && ! is_numeric($expires)) {
146
$expires = strtotime($expires);
147
}
148
149
// httponly added in PHP 5.2.0.
150
// When TRUE the cookie will be made accessible only through the HTTP protocol.
151
// This means that the cookie won't be accessible by scripting languages, such as JavaScript.
152
if (strlen($value) && $httponly) {
153
setcookie($name, $value, $expires, $path, $domain, $secure, TRUE);
154
return;
155
}
156
// Defines a cookie to be sent along with the rest of the HTTP headers
157
// Like other headers, cookies must be sent before any output from your script
158
setcookie($name, $value, $expires, $path, $domain, $secure);
159
}
160
161
162
/**
163
* Sets the default domain to use for cookies
164
*
165
* This value will be used when the `$domain` parameter of the ::set()
166
* method is not specified or is set to `NULL`.
167
*
168
* @param string $domain The default domain to use for cookies
169
* @return void
170
*/
171
public static function set_default_domain($domain) {
172
self::$_default_domain = $domain;
173
}
174
175
176
/**
177
* Sets the default expiration date to use for cookies
178
*
179
* This value will be used when the `$expires` parameter of the ::set()
180
* method is not specified or is set to `NULL`.
181
*
182
* @param string|integer $expires The default expiration date to use for cookies
183
* @return void
184
*/
185
public static function set_default_expires($expires) {
186
self::$_default_expires = $expires;
187
}
188
189
190
/**
191
* Sets the default httponly flag to use for cookies
192
*
193
* This value will be used when the `$httponly` parameter of the ::set()
194
* method is not specified or is set to `NULL`.
195
*
196
* @param boolean $httponly The default httponly flag to use for cookies
197
* @return void
198
*/
199
public static function set_default_httponly($httponly) {
200
self::$_default_httponly = $httponly;
201
}
202
203
204
/**
205
* Sets the default path to use for cookies
206
*
207
* This value will be used when the `$path` parameter of the ::set()
208
* method is not specified or is set to `NULL`.
209
*
210
* @param string $path The default path to use for cookies
211
* @return void
212
*/
213
public static function set_default_path($path) {
214
self::$_default_path = $path;
215
}
216
217
218
/**
219
* Sets the default secure flag to use for cookies
220
*
221
* This value will be used when the `$secure` parameter of the ::set()
222
* method is not specified or is set to `NULL`.
223
*
224
* @param boolean $secure The default secure flag to use for cookies
225
* @return void
226
*/
227
public static function set_default_secure($secure) {
228
self::$_default_secure = $secure;
229
}
230
231
}
232
233
/* End of file: ./system/core/cookie.php */
Page URI: http://www.infopotato.com/index.php/code/core/cookie/
