class CookieJar implements Cookie_Backend (View source)

A default backend for the setting and getting of cookies

This backend allows one to better test Cookie setting and separate cookie handling from the core

Properties

protected array $existing

Hold the cookies that were existing at time of instantiation (ie: The ones sent to PHP by the browser)

protected array $current

Hold the current cookies (ie: a mix of those that were sent to us and we have set without the ones we've cleared)

protected array $new

Hold any NEW cookies that were set by the application and will be sent in the next response

Methods

public
__construct(array $cookies = [])

No description

public
void
set(string $name, string|false $value, int $expiry = 90, string|null $path = null, string|null $domain = null, bool $secure = false, bool $httpOnly = true, string $sameSite = '')

No description

public
string|null
get(string $name, bool $includeUnsent = true)

Get the cookie value by name

public
array
getAll(bool $includeUnsent = true)

No description

public
void
forceExpiry(string $name, string|null $path = null, string|null $domain = null, bool $secure = false, bool $httpOnly = true, string $sameSite = '')

No description

protected
bool
outputCookie(string $name, string|false $value, int $expiry = 90, string|null $path = null, string|null $domain = null, bool $secure = false, bool $httpOnly = true, string $sameSite = '')

The function that actually sets the cookie using PHP

Details

__construct(array $cookies = [])

No description

Parameters

array $cookies

The existing cookies to load into the cookie jar

void set(string $name, string|false $value, int $expiry = 90, string|null $path = null, string|null $domain = null, bool $secure = false, bool $httpOnly = true, string $sameSite = '')

No description

Parameters

string $name

The name of the cookie

string|false $value

The value for the cookie to hold. Empty string or false will clear the cookie

int $expiry

The number of days until expiry; 0 means it will expire at the end of the session

string|null $path

The path to save the cookie on (falls back to site base)

string|null $domain

The domain to make the cookie available on

bool $secure

Can the cookie only be sent over SSL?

bool $httpOnly

Prevent the cookie being accessible by JS

string $sameSite

The "SameSite" value for the cookie. Must be one of "None", "Lax", or "Strict". If $sameSite is left empty, the default will be used.

Return Value

void

string|null get(string $name, bool $includeUnsent = true)

Get the cookie value by name

Cookie names are normalised to work around PHP's behaviour of replacing incoming variable name . with _

Parameters

string $name

The name of the cookie to get

bool $includeUnsent

Include cookies we've yet to send when fetching values

Return Value

string|null

The cookie value or null if unset

array getAll(bool $includeUnsent = true)

No description

Parameters

bool $includeUnsent

Include cookies we've yet to send

Return Value

array

All the cookies

void forceExpiry(string $name, string|null $path = null, string|null $domain = null, bool $secure = false, bool $httpOnly = true, string $sameSite = '')

No description

Parameters

string $name

The name of the cookie to expire

string|null $path

The path to save the cookie on (falls back to site base)

string|null $domain

The domain to make the cookie available on

bool $secure

Can the cookie only be sent over SSL?

bool $httpOnly

Prevent the cookie being accessible by JS

string $sameSite

The "SameSite" value for the cookie. Must be one of "None", "Lax", or "Strict". If $sameSite is left empty, the default will be used.

Return Value

void

protected bool outputCookie(string $name, string|false $value, int $expiry = 90, string|null $path = null, string|null $domain = null, bool $secure = false, bool $httpOnly = true, string $sameSite = '')

The function that actually sets the cookie using PHP

Parameters

string $name

The name of the cookie

string|false $value

The value for the cookie to hold. Empty string or false will clear the cookie.

int $expiry

A Unix timestamp indicating when the cookie expires; 0 means it will expire at the end of the session

string|null $path

The path to save the cookie on (falls back to site base)

string|null $domain

The domain to make the cookie available on

bool $secure

Can the cookie only be sent over SSL?

bool $httpOnly

Prevent the cookie being accessible by JS

string $sameSite

The "SameSite" value for the cookie. Must be one of "None", "Lax", or "Strict". If $sameSite is left empty, the default will be used.

Return Value

bool

If the cookie was set or not; doesn't mean it's accepted by the browser

See also

http://uk3.php.net/manual/en/function.setcookie.php