8 * This source file is subject to the new BSD license that is bundled
9 * with this package in the file LICENSE.txt.
10 * It is also available through the world-wide-web at this URL:
11 * http://framework.zend.com/license/new-bsd
12 * If you did not receive a copy of the license and are unable to
13 * obtain it through the world-wide-web, please send an email
14 * to license@zend.com so we can send you a copy immediately.
19 * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com/)
21 * @license http://framework.zend.com/license/new-bsd New BSD License
24 require_once 'Zend/Uri/Http.php';
28 * Zend_Http_Cookie is a class describing an HTTP cookie and all it's parameters.
30 * Zend_Http_Cookie is a class describing an HTTP cookie and all it's parameters. The
31 * class also enables validating whether the cookie should be sent to the server in
32 * a specified scenario according to the request URI, the expiry time and whether
33 * session cookies should be used or not. Generally speaking cookies should be
34 * contained in a Cookiejar object, or instantiated manually and added to an HTTP
37 * See http://wp.netscape.com/newsref/std/cookie_spec.html for some specs.
41 * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com/)
42 * @license http://framework.zend.com/license/new-bsd New BSD License
44 class Zend_Http_Cookie
82 * Whether the cookie is secure or not
89 * Cookie object constructor
91 * @todo Add validation of each one of the parameters (legal domain, etc.)
94 * @param string $value
95 * @param string $domain
100 public function __construct($name, $value, $domain, $expires = null, $path = null, $secure = false)
102 if (preg_match("/[=,; \t\r\n\013\014]/", $name)) {
103 require_once 'Zend/Http/Exception.php';
104 throw new Zend_Http_Exception("Cookie name cannot contain these characters: =,; \\t\\r\\n\\013\\014 ({$name})");
107 if (! $this->name
= (string) $name) {
108 require_once 'Zend/Http/Exception.php';
109 throw new Zend_Http_Exception('Cookies must have a name');
112 if (! $this->domain
= (string) $domain) {
113 require_once 'Zend/Http/Exception.php';
114 throw new Zend_Http_Exception('Cookies must have a domain');
117 $this->value
= (string) $value;
118 $this->expires
= ($expires === null ?
null : (int) $expires);
119 $this->path
= ($path ?
$path : '/');
120 $this->secure
= $secure;
128 public function getName()
138 public function getValue()
148 public function getDomain()
150 return $this->domain
;
154 * Get the cookie path
158 public function getPath()
164 * Get the expiry time of the cookie, or null if no expiry time is set
168 public function getExpiryTime()
170 return $this->expires
;
174 * Check whether the cookie should only be sent over secure connections
178 public function isSecure()
180 return $this->secure
;
184 * Check whether the cookie has expired
186 * Always returns false if the cookie is a session cookie (has no expiry time)
188 * @param int $now Timestamp to consider as "now"
191 public function isExpired($now = null)
193 if ($now === null) $now = time();
194 if (is_int($this->expires
) && $this->expires
< $now) {
202 * Check whether the cookie is a session cookie (has no expiry time set)
206 public function isSessionCookie()
208 return ($this->expires
=== null);
212 * Checks whether the cookie should be sent or not in a specific scenario
214 * @param string|Zend_Uri_Http $uri URI to check against (secure, domain, path)
215 * @param boolean $matchSessionCookies Whether to send session cookies
216 * @param int $now Override the current time when checking for expiry time
219 public function match($uri, $matchSessionCookies = true, $now = null)
221 if (is_string ($uri)) {
222 $uri = Zend_Uri_Http
::factory($uri);
225 // Make sure we have a valid Zend_Uri_Http object
226 if (! ($uri->valid() && ($uri->getScheme() == 'http' ||
$uri->getScheme() =='https'))) {
227 require_once 'Zend/Http/Exception.php';
228 throw new Zend_Http_Exception('Passed URI is not a valid HTTP or HTTPS URI');
231 // Check that the cookie is secure (if required) and not expired
232 if ($this->secure
&& $uri->getScheme() != 'https') return false;
233 if ($this->isExpired($now)) return false;
234 if ($this->isSessionCookie() && ! $matchSessionCookies) return false;
236 // Validate domain and path
237 // Domain is validated using tail match, while path is validated using head match
238 $domain_preg = preg_quote($this->getDomain(), "/");
239 if (! preg_match("/{$domain_preg}$/", $uri->getHost())) return false;
240 $path_preg = preg_quote($this->getPath(), "/");
241 if (! preg_match("/^{$path_preg}/", $uri->getPath())) return false;
243 // If we didn't die until now, return true.
248 * Get the cookie as a string, suitable for sending as a "Cookie" header in an
253 public function __toString()
255 return $this->name
. '=' . urlencode($this->value
) . ';';
259 * Generate a new Cookie object from a cookie string
260 * (for example the value of the Set-Cookie HTTP header)
262 * @param string $cookieStr
263 * @param Zend_Uri_Http|string $ref_uri Reference URI for default values (domain, path)
264 * @return Zend_Http_Cookie A new Zend_Http_Cookie object or false on failure.
266 public static function fromString($cookieStr, $ref_uri = null)
268 // Set default values
269 if (is_string($ref_uri)) {
270 $ref_uri = Zend_Uri_Http
::factory($ref_uri);
279 $parts = explode(';', $cookieStr);
281 // If first part does not include '=', fail
282 if (strpos($parts[0], '=') === false) return false;
284 // Get the name and value of the cookie
285 list($name, $value) = explode('=', trim(array_shift($parts)), 2);
287 $value = urldecode(trim($value));
289 // Set default domain and path
290 if ($ref_uri instanceof Zend_Uri_Http
) {
291 $domain = $ref_uri->getHost();
292 $path = $ref_uri->getPath();
293 $path = substr($path, 0, strrpos($path, '/'));
296 // Set other cookie parameters
297 foreach ($parts as $part) {
299 if (strtolower($part) == 'secure') {
304 $keyValue = explode('=', $part, 2);
305 if (count($keyValue) == 2) {
306 list($k, $v) = $keyValue;
307 switch (strtolower($k)) {
309 if(($expires = strtotime($v)) === false) {
311 * The expiration is past Tue, 19 Jan 2038 03:14:07 UTC
312 * the maximum for 32-bit signed integer. Zend_Date
313 * can get around that limit.
317 require_once 'Zend/Date.php';
319 $expireDate = new Zend_Date($v);
320 $expires = $expireDate->getTimestamp();
339 return new self($name, $value, $domain, $expires, $path, $secure);