123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152 |
- <?php namespace Laravel; defined('APP_PATH') or die('No direct script access.');
- use Closure;
- if (trim(Config::get('application.key')) === '')
- {
- throw new \Exception('The cookie class may not be used without an application key.');
- }
- class Cookie {
- /**
- * Determine if a cookie exists.
- *
- * @param string $name
- * @return bool
- */
- public static function has($name)
- {
- return ! is_null(static::get($name));
- }
- /**
- * Get the value of a cookie.
- *
- * <code>
- * // Get the value of the "favorite" cookie
- * $favorite = Cookie::get('favorite');
- *
- * // Get the value of a cookie or return a default value if it doesn't exist
- * $favorite = Cookie::get('framework', 'Laravel');
- * </code>
- *
- * @param string $name
- * @param mixed $default
- * @return string
- */
- public static function get($name, $default = null)
- {
- $value = array_get($_COOKIE, $name);
- if ( ! is_null($value) and isset($value[40]) and $value[40] == '~')
- {
- // The hash signature and the cookie value are separated by a tilde
- // character for convenience. To separate the hash and the contents
- // we can simply expode on that character.
- //
- // By re-feeding the cookie value into the "sign" method, we should
- // be able to generate a hash that matches the one taken out of the
- // cookie. If they don't match, the cookie value has been changed.
- list($hash, $value) = explode('~', $value, 2);
- if (static::hash($name, $value) === $hash)
- {
- return $value;
- }
- }
- return value($default);
- }
- /**
- * Set the value of a cookie.
- *
- * If the response headers have already been sent, the cookie will not be set.
- *
- * <code>
- * // Set the value of the "favorite" cookie
- * Cookie::put('favorite', 'Laravel');
- *
- * // Set the value of the "favorite" cookie for twenty minutes
- * Cookie::put('favorite', 'Laravel', 20);
- * </code>
- *
- * @param string $name
- * @param string $value
- * @param int $minutes
- * @param string $path
- * @param string $domain
- * @param bool $secure
- * @return bool
- */
- public static function put($name, $value, $minutes = 0, $path = '/', $domain = null, $secure = false)
- {
- if (headers_sent()) return false;
- $time = ($minutes !== 0) ? time() + ($minutes * 60) : 0;
- $_COOKIE[$name] = static::sign($name, $value);
- return setcookie($name, $_COOKIE[$name], $time, $path, $domain, $secure);
- }
- /**
- * Set a "permanent" cookie. The cookie will last for one year.
- *
- * <code>
- * // Set a cookie that should last one year
- * Cookie::forever('favorite', 'Blue');
- * </code>
- *
- * @param string $name
- * @param string $value
- * @param string $path
- * @param string $domain
- * @param bool $secure
- * @return bool
- */
- public static function forever($name, $value, $path = '/', $domain = null, $secure = false)
- {
- return static::put($name, $value, 525600, $path, $domain, $secure);
- }
- /**
- * Generate a cookie signature based on the contents.
- *
- * @param string $name
- * @param string $value
- * @return string
- */
- protected static function sign($name, $value)
- {
- return static::hash($name, $value).'~'.$value;
- }
- /**
- * Generate a cookie hash based on the contents.
- *
- * @param string $name
- * @param string $value
- * @return string
- */
- protected static function hash($name, $value)
- {
- return sha1($name.$value.Config::get('application.key'));
- }
- /**
- * Delete a cookie.
- *
- * @param string $name
- * @param string $path
- * @param string $domain
- * @param bool $secure
- * @return bool
- */
- public static function forget($name, $path = '/', $domain = null, $secure = false)
- {
- return static::put($name, null, -2000, $path, $domain, $secure);
- }
- }
|