cookie.php 2.81 KB
Newer Older
1
<?php namespace Laravel;
2

3 4 5
class Cookie {

	/**
6 7 8 9 10 11 12
	 * How long is forever (in minutes).
	 *
	 * @var int
	 */
	const forever = 525600;

	/**
13 14 15 16 17 18 19
	 * The cookies that have been set.
	 *
	 * @var array
	 */
	public static $jar = array();

	/**
20 21
	 * Determine if a cookie exists.
	 *
22
	 * @param  string  $name
23 24
	 * @return bool
	 */
25
	public static function has($name)
26
	{
27
		return ! is_null(static::get($name));
28 29 30 31 32
	}

	/**
	 * Get the value of a cookie.
	 *
33 34 35 36
	 * <code>
	 *		// Get the value of the "favorite" cookie
	 *		$favorite = Cookie::get('favorite');
	 *
37
	 *		// Get the value of a cookie or return a default value
38 39 40
	 *		$favorite = Cookie::get('framework', 'Laravel');
	 * </code>
	 *
41
	 * @param  string  $name
42 43 44
	 * @param  mixed   $default
	 * @return string
	 */
45
	public static function get($name, $default = null)
46
	{
Taylor Otwell committed
47
		if (isset(static::$jar[$name])) return static::$jar[$name]['value'];
48

49
		return array_get(Request::foundation()->cookies->all(), $name, $default);
50 51 52
	}

	/**
53 54 55 56 57 58 59 60 61
	 * 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>
62
	 *
63 64
	 * @param  string  $name
	 * @param  string  $value
65
	 * @param  int     $expiration
66 67 68
	 * @param  string  $path
	 * @param  string  $domain
	 * @param  bool    $secure
Phill Sparks committed
69
	 * @return void
70
	 */
71
	public static function put($name, $value, $expiration = 0, $path = '/', $domain = null, $secure = false)
72
	{
73 74 75 76 77
		if ($expiration !== 0)
		{
			$expiration = time() + ($expiration * 60);
		}

Taylor Otwell committed
78 79 80 81 82 83 84 85
		// 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.");
		}

86
		static::$jar[$name] = compact('name', 'value', 'expiration', 'path', 'domain', 'secure');
87 88 89
	}

	/**
90
	 * Set a "permanent" cookie. The cookie will last for one year.
91
	 *
92 93 94 95
	 * <code>
	 *		// Set a cookie that should last one year
	 *		Cookie::forever('favorite', 'Blue');
	 * </code>
96
	 *
97 98 99 100 101
	 * @param  string  $name
	 * @param  string  $value
	 * @param  string  $path
	 * @param  string  $domain
	 * @param  bool    $secure
102 103
	 * @return bool
	 */
104
	public static function forever($name, $value, $path = '/', $domain = null, $secure = false)
105
	{
106
		return static::put($name, $value, static::forever, $path, $domain, $secure);
Taylor Otwell committed
107 108 109
	}

	/**
110 111
	 * Delete a cookie.
	 *
112
	 * @param  string  $name
113 114 115
	 * @param  string  $path
	 * @param  string  $domain
	 * @param  bool    $secure
116 117
	 * @return bool
	 */
118
	public static function forget($name, $path = '/', $domain = null, $secure = false)
119
	{
120
		return static::put($name, null, -2000, $path, $domain, $secure);
121 122
	}

123
}