cookie.php 1.66 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 32 33 34 35 36
	 * @param  string  $name
	 * @param  string  $value
	 * @param  string  $path
	 * @param  string  $domain
	 * @param  bool    $secure
	 * @param  bool    $http_only
37 38
	 * @return bool
	 */
39
	public static function forever($name, $value, $path = '/', $domain = null, $secure = false, $http_only = false)
40
	{
41
		return static::put($name, $value, 2628000, $path, $domain, $secure, $http_only);
42 43 44
	}

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

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

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

}