cookie.php 1.55 KB
Newer Older
1 2 3 4 5 6 7
<?php namespace System;

class Cookie {

	/**
	 * Determine if a cookie exists.
	 *
8
	 * @param  string  $name
9 10
	 * @return bool
	 */
11
	public static function has($name)
12
	{
Taylor Otwell committed
13
		return ! is_null(static::get($name));
14 15 16 17 18
	}

	/**
	 * Get the value of a cookie.
	 *
19
	 * @param  string  $name
20 21 22
	 * @param  mixed   $default
	 * @return string
	 */
23
	public static function get($name, $default = null)
24
	{
25
		return Arr::get($_COOKIE, $name, $default);
26 27 28 29 30
	}

	/**
	 * Set a "permanent" cookie. The cookie will last 5 years.
	 *
31
	 * @param  string   $name
32 33 34 35 36 37
	 * @param  string   $value
	 * @param  string   $path
	 * @param  string   $domain
	 * @param  bool     $secure
	 * @return bool
	 */
38
	public static function forever($name, $value, $path = '/', $domain = null, $secure = false)
39
	{
40
		return static::put($name, $value, 2628000, $path, $domain, $secure);
41 42 43
	}

	/**
44 45
	 * Set the value of a cookie. If a negative number of minutes is
	 * specified, the cookie will be deleted.
46
	 *
47
	 * @param  string   $name
48 49 50 51 52 53 54
	 * @param  string   $value
	 * @param  int      $minutes
	 * @param  string   $path
	 * @param  string   $domain
	 * @param  bool     $secure
	 * @return bool
	 */
55
	public static function put($name, $value, $minutes = 0, $path = '/', $domain = null, $secure = false)
56 57 58
	{
		if ($minutes < 0)
		{
59
			unset($_COOKIE[$name]);
60 61
		}

62
		return setcookie($name, $value, ($minutes != 0) ? time() + ($minutes * 60) : 0, $path, $domain, $secure);
63 64 65 66 67
	}

	/**
	 * Delete a cookie.
	 *
68
	 * @param  string  $name
69 70
	 * @return bool
	 */
71
	public static function forget($name)
72 73 74 75 76
	{
		return static::put($key, null, -60);
	}

}