1 <?php
defined('SYSPATH') or die('No direct script access.');
8 * @copyright (c) 2008-2010 Kohana Team
9 * @license http://kohanaframework.org/license
14 * @var string Magic salt to add to the cookie
16 public static $salt = NULL;
19 * @var integer Number of seconds before the cookie expires
21 public static $expiration = 0;
24 * @var string Restrict the path that the cookie is available to
26 public static $path = '/';
29 * @var string Restrict the domain that the cookie is available to
31 public static $domain = NULL;
34 * @var boolean Only transmit cookies over secure connections
36 public static $secure = FALSE;
39 * @var boolean Only transmit cookies over HTTP, disabling Javascript access
41 public static $httponly = FALSE;
44 * Gets the value of a signed cookie. Cookies without signatures will not
45 * be returned. If the cookie signature is present, but invalid, the cookie
48 * // Get the "theme" cookie, or use "blue" if the cookie does not exist
49 * $theme = Cookie::get('theme', 'blue');
51 * @param string cookie name
52 * @param mixed default value to return
55 public static function get($key, $default = NULL)
57 if ( ! isset($_COOKIE[$key]))
59 // The cookie does not exist
63 // Get the cookie value
64 $cookie = $_COOKIE[$key];
66 // Find the position of the split between salt and contents
67 $split = strlen(Cookie
::salt($key, NULL));
69 if (isset($cookie[$split]) AND $cookie[$split] === '~')
71 // Separate the salt and the value
72 list ($hash, $value) = explode('~', $cookie, 2);
74 if (Cookie
::salt($key, $value) === $hash)
76 // Cookie signature is valid
80 // The cookie signature is invalid, delete it
88 * Sets a signed cookie. Note that all cookie values must be strings and no
89 * automatic serialization will be performed!
91 * // Set the "theme" cookie
92 * Cookie::set('theme', 'red');
94 * @param string name of cookie
95 * @param string value of cookie
96 * @param integer lifetime in seconds
100 public static function set($name, $value, $expiration = NULL)
102 if ($expiration === NULL)
104 // Use the default expiration
105 $expiration = Cookie
::$expiration;
108 if ($expiration !== 0)
110 // The expiration is expected to be a UNIX timestamp
111 $expiration +
= time();
114 // Add the salt to the cookie value
115 $value = Cookie
::salt($name, $value).'~'.$value;
117 return setcookie($name, $value, $expiration, Cookie
::$path, Cookie
::$domain, Cookie
::$secure, Cookie
::$httponly);
121 * Deletes a cookie by making the value NULL and expiring it.
123 * Cookie::delete('theme');
125 * @param string cookie name
129 public static function delete($name)
132 unset($_COOKIE[$name]);
134 // Nullify the cookie and make it expire
135 return Cookie
::set($name, NULL, -86400);
139 * Generates a salt string for a cookie based on the name and value.
141 * $salt = Cookie::salt('theme', 'red');
143 * @param string name of cookie
144 * @param string value of cookie
147 public static function salt($name, $value)
149 // Require a valid salt
150 if ( ! Cookie
::$salt)
152 throw new Kohana_Exception('A valid cookie salt is required. Please set Cookie::$salt.');
155 // Determine the user agent
156 $agent = isset($_SERVER['HTTP_USER_AGENT']) ?
strtolower($_SERVER['HTTP_USER_AGENT']) : 'unknown';
158 return sha1($agent.$name.$value.Cookie
::$salt);