2012-03-28 22:43:58 -05:00
|
|
|
<?php namespace Laravel; use Closure;
|
2012-01-16 13:59:24 -06:00
|
|
|
|
2011-06-08 23:45:08 -05:00
|
|
|
class Cookie {
|
|
|
|
|
|
2012-01-31 15:19:23 -06:00
|
|
|
/**
|
|
|
|
|
* The cookies that have been set.
|
|
|
|
|
*
|
|
|
|
|
* @var array
|
|
|
|
|
*/
|
|
|
|
|
public static $jar = array();
|
|
|
|
|
|
2011-06-08 23:45:08 -05:00
|
|
|
/**
|
|
|
|
|
* Determine if a cookie exists.
|
|
|
|
|
*
|
2011-06-15 10:23:38 -07:00
|
|
|
* @param string $name
|
2011-06-08 23:45:08 -05:00
|
|
|
* @return bool
|
|
|
|
|
*/
|
2011-10-05 18:32:48 -05:00
|
|
|
public static function has($name)
|
2011-06-08 23:45:08 -05:00
|
|
|
{
|
2011-10-05 18:32:48 -05:00
|
|
|
return ! is_null(static::get($name));
|
2011-06-08 23:45:08 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get the value of a cookie.
|
|
|
|
|
*
|
2012-01-16 13:59:24 -06:00
|
|
|
* <code>
|
|
|
|
|
* // Get the value of the "favorite" cookie
|
|
|
|
|
* $favorite = Cookie::get('favorite');
|
|
|
|
|
*
|
2012-01-31 15:58:00 -06:00
|
|
|
* // Get the value of a cookie or return a default value
|
2012-01-16 13:59:24 -06:00
|
|
|
* $favorite = Cookie::get('framework', 'Laravel');
|
|
|
|
|
* </code>
|
|
|
|
|
*
|
2011-06-15 10:23:38 -07:00
|
|
|
* @param string $name
|
2011-06-08 23:45:08 -05:00
|
|
|
* @param mixed $default
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
2011-10-05 18:32:48 -05:00
|
|
|
public static function get($name, $default = null)
|
2011-06-08 23:45:08 -05:00
|
|
|
{
|
2012-03-28 22:43:58 -05:00
|
|
|
if (isset(static::$jar[$name])) return static::$jar[$name];
|
2012-01-31 15:58:00 -06:00
|
|
|
|
2012-03-28 22:43:58 -05:00
|
|
|
return array_get(Request::foundation()->cookies->all(), $name, $default);
|
2011-06-08 23:45:08 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2012-01-16 13:59:24 -06:00
|
|
|
* Set the value of a cookie.
|
|
|
|
|
*
|
|
|
|
|
* <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>
|
2011-06-08 23:45:08 -05:00
|
|
|
*
|
2011-07-21 23:54:25 -05:00
|
|
|
* @param string $name
|
|
|
|
|
* @param string $value
|
2012-03-28 22:43:58 -05:00
|
|
|
* @param int $expiration
|
2011-07-21 23:54:25 -05:00
|
|
|
* @param string $path
|
|
|
|
|
* @param string $domain
|
|
|
|
|
* @param bool $secure
|
2012-02-04 21:30:52 +00:00
|
|
|
* @return void
|
2011-06-08 23:45:08 -05:00
|
|
|
*/
|
2012-03-28 22:43:58 -05:00
|
|
|
public static function put($name, $value, $expiration = 0, $path = '/', $domain = null, $secure = false)
|
2011-06-08 23:45:08 -05:00
|
|
|
{
|
2012-03-28 22:43:58 -05:00
|
|
|
if ($expiration !== 0)
|
|
|
|
|
{
|
|
|
|
|
$expiration = time() + ($expiration * 60);
|
|
|
|
|
}
|
|
|
|
|
|
2012-04-04 10:28:06 -05:00
|
|
|
// If the secure option is set to true, yet the request is not over HTTPS
|
|
|
|
|
// we'll throw an exception to let the developer know that they are
|
|
|
|
|
// attempting to send a secure cookie over the unsecure HTTP.
|
|
|
|
|
if ($secure and ! Request::secure())
|
|
|
|
|
{
|
|
|
|
|
throw new \Exception("Attempting to set secure cookie over HTTP.");
|
|
|
|
|
}
|
|
|
|
|
|
2012-03-28 22:43:58 -05:00
|
|
|
static::$jar[$name] = compact('name', 'value', 'expiration', 'path', 'domain', 'secure');
|
2011-06-08 23:45:08 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2012-01-16 13:59:24 -06:00
|
|
|
* Set a "permanent" cookie. The cookie will last for one year.
|
2011-06-08 23:45:08 -05:00
|
|
|
*
|
2012-01-16 13:59:24 -06:00
|
|
|
* <code>
|
|
|
|
|
* // Set a cookie that should last one year
|
|
|
|
|
* Cookie::forever('favorite', 'Blue');
|
|
|
|
|
* </code>
|
2011-09-09 20:55:24 -05:00
|
|
|
*
|
2011-07-21 23:54:25 -05:00
|
|
|
* @param string $name
|
|
|
|
|
* @param string $value
|
|
|
|
|
* @param string $path
|
|
|
|
|
* @param string $domain
|
|
|
|
|
* @param bool $secure
|
2011-06-08 23:45:08 -05:00
|
|
|
* @return bool
|
|
|
|
|
*/
|
2012-01-16 13:59:24 -06:00
|
|
|
public static function forever($name, $value, $path = '/', $domain = null, $secure = false)
|
2011-06-08 23:45:08 -05:00
|
|
|
{
|
2012-01-16 13:59:24 -06:00
|
|
|
return static::put($name, $value, 525600, $path, $domain, $secure);
|
2011-10-10 21:34:15 -05:00
|
|
|
}
|
|
|
|
|
|
2011-06-08 23:45:08 -05:00
|
|
|
/**
|
|
|
|
|
* Delete a cookie.
|
|
|
|
|
*
|
2011-06-15 10:23:38 -07:00
|
|
|
* @param string $name
|
2012-01-05 11:52:42 -06:00
|
|
|
* @param string $path
|
|
|
|
|
* @param string $domain
|
|
|
|
|
* @param bool $secure
|
2011-06-08 23:45:08 -05:00
|
|
|
* @return bool
|
|
|
|
|
*/
|
2012-01-16 13:59:24 -06:00
|
|
|
public static function forget($name, $path = '/', $domain = null, $secure = false)
|
2011-06-08 23:45:08 -05:00
|
|
|
{
|
2012-01-16 13:59:24 -06:00
|
|
|
return static::put($name, null, -2000, $path, $domain, $secure);
|
2011-06-08 23:45:08 -05:00
|
|
|
}
|
|
|
|
|
|
2011-11-18 10:43:37 -06:00
|
|
|
}
|