2011-08-18 19:56:29 -05:00
|
|
|
<?php namespace Laravel;
|
2011-06-08 23:45:08 -05:00
|
|
|
|
|
|
|
|
class Cookie {
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 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.
|
|
|
|
|
*
|
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
|
|
|
{
|
2011-10-05 18:32:48 -05:00
|
|
|
return Arr::get($_COOKIE, $name, $default);
|
2011-06-08 23:45:08 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Set a "permanent" cookie. The cookie will last 5 years.
|
|
|
|
|
*
|
2011-07-21 23:54:25 -05:00
|
|
|
* @param string $name
|
|
|
|
|
* @param string $value
|
|
|
|
|
* @param string $path
|
|
|
|
|
* @param string $domain
|
|
|
|
|
* @param bool $secure
|
|
|
|
|
* @param bool $http_only
|
2011-06-08 23:45:08 -05:00
|
|
|
* @return bool
|
|
|
|
|
*/
|
2011-10-05 18:32:48 -05:00
|
|
|
public static function forever($name, $value, $path = '/', $domain = null, $secure = false, $http_only = false)
|
2011-06-08 23:45:08 -05:00
|
|
|
{
|
2011-10-05 18:32:48 -05:00
|
|
|
return static::put($name, $value, 2628000, $path, $domain, $secure, $http_only);
|
2011-06-08 23:45:08 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2011-09-08 17:49:16 -05:00
|
|
|
* Set the value of a cookie.
|
|
|
|
|
*
|
|
|
|
|
* If a negative number of minutes is specified, the cookie will be deleted.
|
2011-06-08 23:45:08 -05:00
|
|
|
*
|
2011-09-09 20:55:24 -05:00
|
|
|
* Note: This method's signature is very similar to the PHP setcookie method.
|
|
|
|
|
* However, you simply need to pass the number of minutes for which you
|
|
|
|
|
* wish the cookie to be valid. No funky time calculation is required.
|
|
|
|
|
*
|
2011-07-21 23:54:25 -05:00
|
|
|
* @param string $name
|
|
|
|
|
* @param string $value
|
|
|
|
|
* @param int $minutes
|
|
|
|
|
* @param string $path
|
|
|
|
|
* @param string $domain
|
|
|
|
|
* @param bool $secure
|
|
|
|
|
* @param bool $http_only
|
2011-06-08 23:45:08 -05:00
|
|
|
* @return bool
|
|
|
|
|
*/
|
2011-10-05 18:32:48 -05:00
|
|
|
public static function put($name, $value, $minutes = 0, $path = '/', $domain = null, $secure = false, $http_only = false)
|
2011-06-08 23:45:08 -05:00
|
|
|
{
|
2011-09-20 23:36:13 -05:00
|
|
|
if (headers_sent()) return false;
|
|
|
|
|
|
2011-10-05 18:32:48 -05:00
|
|
|
if ($minutes < 0) unset($_COOKIE[$name]);
|
2011-09-08 17:49:16 -05:00
|
|
|
|
2011-09-21 22:45:50 -05:00
|
|
|
// Since PHP needs the cookie lifetime in seconds, we will calculate it here.
|
|
|
|
|
// A "0" lifetime means the cookie expires when the browser closes.
|
|
|
|
|
$time = ($minutes !== 0) ? time() + ($minutes * 60) : 0;
|
2011-06-08 23:45:08 -05:00
|
|
|
|
2011-09-20 23:36:13 -05:00
|
|
|
return setcookie($name, $value, $time, $path, $domain, $secure, $http_only);
|
2011-06-08 23:45:08 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Delete a cookie.
|
|
|
|
|
*
|
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 forget($name)
|
2011-06-08 23:45:08 -05:00
|
|
|
{
|
2011-10-05 18:32:48 -05:00
|
|
|
return static::put($name, null, -60);
|
2011-06-08 23:45:08 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|