123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131 |
- <?php namespace Laravel; use Closure;
- if (trim(Config::get('application.key')) === '')
- {
- throw new \Exception('The cookie class may not be used without an application key.');
- }
- class Cookie {
-
- public static function has($name)
- {
- return ! is_null(static::get($name));
- }
-
- public static function get($name, $default = null)
- {
- $value = Arr::get($_COOKIE, $name);
- if ( ! is_null($value))
- {
-
-
-
-
-
- if (isset($value[40]) and $value[40] === '~')
- {
- list($hash, $value) = explode('~', $value, 2);
- if (static::hash($name, $value) === $hash)
- {
- return $value;
- }
- }
- }
- return ($default instanceof Closure) ? call_user_func($default) : $default;
- }
-
- public static function forever($name, $value, $path = '/', $domain = null, $secure = false, $http_only = false)
- {
- return static::put($name, $value, 525600, $path, $domain, $secure, $http_only);
- }
-
- public static function put($name, $value, $minutes = 0, $path = '/', $domain = null, $secure = false, $http_only = false)
- {
- if (headers_sent()) return false;
- if ($minutes < 0) unset($_COOKIE[$name]);
- $time = ($minutes !== 0) ? time() + ($minutes * 60) : 0;
- $value = static::hash($name, $value).'~'.$value;
- return setcookie($name, $value, $time, $path, $domain, $secure, $http_only);
- }
-
- protected static function hash($name, $value)
- {
- return sha1($name.$value.Config::get('application.key'));
- }
-
- public static function forget($name)
- {
- return static::put($name, null, -2000);
- }
- }
|