Class Cookie

Cookie

extends Kohana_Cookie

Cookie helper.

package
Kohana
category
Helpers
author
Kohana Team
copyright
(c) Kohana Team
license
https://koseven.ga/LICENSE.md


Information

This class is a transparent base class for Kohana_Cookie

Properties

public static string $domain

Restrict the domain that the cookie is available to

NULL

public static integer $expiration

Number of seconds before the cookie expires

integer 0

public static boolean $httponly

Only transmit cookies over HTTP, disabling Javascript access

bool FALSE

public static string $path

Restrict the path that the cookie is available to

string(1) "/"

public static string $salt

Magic salt to add to the cookie

NULL

public static boolean $secure

Only transmit cookies over secure connections

bool FALSE

Methods

public static delete(string $name ) (defined in Kohana_Cookie)

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

Cookie::delete('theme');

Parameters

  • string $name required - Cookie name

Return Values

  • boolean

Source Code

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

	// Nullify the cookie and make it expire
	return static::_setcookie($name, NULL, -86400, Cookie::$path, Cookie::$domain, Cookie::$secure, Cookie::$httponly);
}

public static get(string $key [, mixed $default = NULL ] ) (defined in Kohana_Cookie)

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.

// Get the "theme" cookie, or use "blue" if the cookie does not exist
$theme = Cookie::get('theme', 'blue');

Parameters

  • string $key required - Cookie name
  • mixed $default = NULL - Default value to return

Return Values

  • string

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 (Security::slow_equals(Cookie::salt($key, $value), $hash))
		{
			// Cookie signature is valid
			return $value;
		}

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

	return $default;
}

public static salt(string $name , string $value ) (defined in Kohana_Cookie)

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

$salt = Cookie::salt('theme', 'red');

Parameters

  • string $name required - Name of cookie
  • string $value required - Value of cookie

Tags

Return Values

  • string

Source Code

public static function salt($name, $value)
{
	// Require a valid salt
	if ( ! Cookie::$salt)
	{
		throw new Kohana_Exception('A valid cookie salt is required. Please set Cookie::$salt in your bootstrap.php. For more information check the documentation');
	}

	// Determine the user agent
	$agent = isset($_SERVER['HTTP_USER_AGENT']) ? strtolower($_SERVER['HTTP_USER_AGENT']) : 'unknown';

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

public static set(string $name , string $value [, integer $lifetime = NULL ] ) (defined in Kohana_Cookie)

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

By default, Cookie::$expiration is 0 - if you skip/pass NULL for the optional lifetime argument your cookies will expire immediately unless you have separately configured Cookie::$expiration.

// Set the "theme" cookie
Cookie::set('theme', 'red');

Parameters

  • string $name required - Name of cookie
  • string $value required - Value of cookie
  • integer $lifetime = NULL - Lifetime in seconds

Tags

Return Values

  • boolean

Source Code

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

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

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

	return static::_setcookie($name, $value, $lifetime, Cookie::$path, Cookie::$domain, Cookie::$secure, Cookie::$httponly);
}

protected static _setcookie(string $name , string $value , integer $expire , string $path , string $domain , boolean $secure , boolean $httponly ) (defined in Kohana_Cookie)

Proxy for the native setcookie function - to allow mocking in unit tests so that they do not fail when headers have been sent.

Parameters

  • string $name required - $name
  • string $value required - $value
  • integer $expire required - $expire
  • string $path required - $path
  • string $domain required - $domain
  • boolean $secure required - $secure
  • boolean $httponly required -

Tags

Return Values

  • bool

Source Code

protected static function _setcookie($name, $value, $expire, $path, $domain, $secure, $httponly)
{
	return setcookie($name, $value, $expire, $path, $domain, $secure, $httponly);
}

protected static _time() (defined in Kohana_Cookie)

Proxy for the native time function - to allow mocking of time-related logic in unit tests

Tags

Return Values

  • int

Source Code

protected static function _time()
{
	return time();
}

Do you want to contribute to Koseven?

We need YOUR help!

This project is open source. What does this mean? YOU can help:
  • Found a bug? Report it on Github
  • Need a feature? Add it Here
  • Want to help? Join the Forum
Go to Github