Cookie
Kohana_Cookie

Class Contents

Class declared in SYSPATH/classes/cookie.php on line 3.

Properties

public static $domain
NULL
public static $expiration
integer 0
public static $httponly
bool FALSE
public static $path
string(1) "/"
public static $salt
string(5) "kooky"
public static $secure
bool FALSE

Methods

public static delete( )
Kohana_Cookie

Source Code
public static function delete($name)
{
	// Remove the cookie
	unset($_COOKIE[$name]);

	// Nullify the cookie and make it expire
	return Cookie::set($name, NULL, -86400);
}

public static get( )
Kohana_Cookie

Source Code
public static function get($key, $default = NULL)
{
	if ( ! isset($_COOKIE[$key]))
	{
		// The cookie does not exist
		return $default;
	}

	// Get the cookie value
	$cookie = $_COOKIE[$key];

	// Find the position of the split between salt and contents
	$split = strlen(Cookie::salt($key, NULL));

	if (isset($cookie[$split]) AND $cookie[$split] === '~')
	{
		// Separate the salt and the value
		list ($hash, $value) = explode('~', $cookie, 2);

		if (Cookie::salt($key, $value) === $hash)
		{
			// Cookie signature is valid
			return $value;
		}

		// The cookie signature is invalid, delete it
		Cookie::delete($key);
	}

	return $default;
}

public static salt( )
Kohana_Cookie

Source Code
public static function salt($name, $value)
{
	// Determine the user agent
	$agent = isset($_SERVER['HTTP_USER_AGENT']) ? strtolower($_SERVER['HTTP_USER_AGENT']) : 'unknown';

	return sha1($agent.$name.$value.Cookie::$salt);
}

public static set( )
Kohana_Cookie

Source Code
public static function set($name, $value, $expiration = NULL)
{
	if ($expiration === NULL)
	{
		// Use the default expiration
		$expiration = Cookie::$expiration;
	}

	if ($expiration !== 0)
	{
		// The expiration is expected to be a UNIX timestamp
		$expiration += time();
	}

	// Add the salt to the cookie value
	$value = Cookie::salt($name, $value).'~'.$value;

	return setcookie($name, $value, $expiration, Cookie::$path, Cookie::$domain, Cookie::$secure, Cookie::$httponly);
}