Cookie
» Kohana_Cookie

Cookie helper.

package
Kohana
author
Kohana Team
copyright
© 2008-2009 Kohana Team
license
http://kohanaphp.com/license.html

Properties

public static string salt
Magic salt to add to the cookie
string(5) "kooky"
public static integer expiration
Number of seconds before the cookie expires
integer 0
public static string path
Restrict the path that the cookie is available to
string(1) "/"
public static string domain
Restrict the domain that the cookie is available to
NULL
public static boolean secure
Only transmit cookies over secure connections
bool FALSE
public static boolean httponly
Only transmit cookies over HTTP, disabling Javascript access
bool FALSE

Methods

public static get ( string $key, mixed $default = NULL )

Gets the value of a signed cookie. Cookies without signatures will not be returned. If the cookie signature is present, but invalid, the cookie will be deleted.

Returns:
  • string
Source:
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 set ( string $name, string $value, integer $expiration = NULL )

Sets a signed cookie. Note that all cookie values must be strings and no automatic serialization will be performed!

Returns:
  • boolean
Source:
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);
}

public static delete ( string $name )

Deletes a cookie by making the value NULL and expiring it.

Returns:
  • boolean
Source:
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 salt ( string $name, string $value )

Generates a salt string for a cookie based on the name and value.

Returns:
  • string
Source:
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);
}

final private __construct ( )

Source:
final private function __construct()
{
	// This is a static class
}