cookie.php 1.67 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
<?php namespace System;

class Cookie {

	/**
	 * Determine if a cookie exists.
	 *
	 * @param  string  $key
	 * @return bool
	 */
	public static function has($key)
	{
		return ( ! is_null(static::get($key)));
	}

	/**
	 * Get the value of a cookie.
	 *
	 * @param  string  $key
	 * @param  mixed   $default
	 * @return string
	 */
	public static function get($key, $default = null)
	{
		return (array_key_exists($key, $_COOKIE)) ? $_COOKIE[$key] : $default;
	}

	/**
	 * Set a "permanent" cookie. The cookie will last 5 years.
	 *
	 * @param  string   $key
	 * @param  string   $value
	 * @param  string   $path
	 * @param  string   $domain
	 * @param  bool     $secure
	 * @return bool
	 */
	public static function forever($key, $value, $path = '/', $domain = null, $secure = false)
	{
		return static::put($key, $value, 2628000, $path, $domain, $secure);
	}

	/**
	 * Set the value of a cookie.
	 *
	 * @param  string   $key
	 * @param  string   $value
	 * @param  int      $minutes
	 * @param  string   $path
	 * @param  string   $domain
	 * @param  bool     $secure
	 * @return bool
	 */
	public static function put($key, $value, $minutes = 0, $path = '/', $domain = null, $secure = false)
	{
		// ----------------------------------------------------------
		// If the lifetime is less than zero, delete the cookie.
		// ----------------------------------------------------------
		if ($minutes < 0)
		{
			unset($_COOKIE[$key]);
		}

		return setcookie($key, $value, ($minutes != 0) ? time() + ($minutes * 60) : 0, $path, $domain, $secure);
	}

	/**
	 * Delete a cookie.
	 *
	 * @param  string  $key
	 * @return bool
	 */
	public static function forget($key)
	{
		return static::put($key, null, -60);
	}

}