\Cake\Http\CookieCookie

Cookie object to build a cookie and turn it into a header value

An HTTP cookie (also called web cookie, Internet cookie, browser cookie or simply cookie) is a small piece of data sent from a website and stored on the user's computer by the user's web browser while the user is browsing.

Cookies were designed to be a reliable mechanism for websites to remember stateful information (such as items added in the shopping cart in an online store) or to record the user's browsing activity (including clicking particular buttons, logging in, or recording which pages were visited in the past). They can also be used to remember arbitrary pieces of information that the user previously entered into form fields such as names, and preferences.

Cookie objects are immutable, and you must re-assign variables when modifying cookie objects:

$cookie = $cookie->withValue('0');

Summary

Methods
Properties
Constants
__construct()
toHeaderValue()
withName()
getId()
getName()
getValue()
getStringValue()
withValue()
withPath()
getPath()
withDomain()
getDomain()
isSecure()
withSecure()
withHttpOnly()
isHttpOnly()
withExpiry()
getExpiry()
getExpiresTimestamp()
getFormattedExpires()
isExpired()
withNeverExpire()
withExpired()
check()
withAddedValue()
withoutAddedValue()
read()
isExpanded()
No public properties found
No constants found
validateName()
_setValue()
validateString()
validateBool()
_flatten()
_expand()
$name
$value
$isExpanded
$expiresAt
$path
$domain
$secure
$httpOnly
N/A
No private methods found
No private properties found
N/A

Properties

$name

$name : string

Cookie name

Type

string

$value

$value : string|array

Raw Cookie value.

Type

string|array

$isExpanded

$isExpanded : boolean

Whether or not a JSON value has been expanded into an array.

Type

boolean

$expiresAt

$expiresAt : \DateTime|\DateTimeImmutable|null

Expiration time

Type

\DateTime|\DateTimeImmutable|null

$path

$path : string

Path

Type

string

$domain

$domain : string

Domain

Type

string

$secure

$secure : boolean

Secure

Type

boolean

$httpOnly

$httpOnly : boolean

HTTP only

Type

boolean

Methods

__construct()

__construct(string  $name, string|array  $value = '', \DateTime|\DateTimeImmutable|null  $expiresAt = null, string  $path = '/', string  $domain = '', boolean  $secure = false, boolean  $httpOnly = false) 

Constructor

The constructors args are similar to the native PHP setcookie() method. The only difference is the 3rd argument which excepts null or an DateTime or DateTimeImmutable object instead an integer.

Parameters

string $name

Cookie name

string|array $value

Value of the cookie

\DateTime|\DateTimeImmutable|null $expiresAt

Expiration time and date

string $path

Path

string $domain

Domain

boolean $secure

Is secure

boolean $httpOnly

HTTP Only

toHeaderValue()

toHeaderValue() : string

Returns a header value as string

Returns

string

withName()

withName(string  $name) : static

Sets the cookie name

Parameters

string $name

Name of the cookie

Returns

static

getId()

getId() : string

Get the id for a cookie

Cookies are unique across name, domain, path tuples.

Returns

string

getName()

getName() : string

Gets the cookie name

Returns

string

getValue()

getValue() : string|array

Gets the cookie value

Returns

string|array

getStringValue()

getStringValue() : string

Gets the cookie value as a string.

This will collapse any complex data in the cookie with json_encode()

Returns

string

withValue()

withValue(string|array  $value) : static

Create a cookie with an updated value.

Parameters

string|array $value

Value of the cookie to set

Returns

static

withPath()

withPath(string  $path) : static

Create a new cookie with an updated path

Parameters

string $path

Sets the path

Returns

static

getPath()

getPath() : string

Get the path attribute.

Returns

string

withDomain()

withDomain(string  $domain) : static

Create a cookie with an updated domain

Parameters

string $domain

Domain to set

Returns

static

getDomain()

getDomain() : string

Get the domain attribute.

Returns

string

isSecure()

isSecure() : boolean

Check if the cookie is secure

Returns

boolean

withSecure()

withSecure(boolean  $secure) : static

Create a cookie with Secure updated

Parameters

boolean $secure

Secure attribute value

Returns

static

withHttpOnly()

withHttpOnly(boolean  $httpOnly) : static

Create a cookie with HTTP Only updated

Parameters

boolean $httpOnly

HTTP Only

Returns

static

isHttpOnly()

isHttpOnly() : boolean

Check if the cookie is HTTP only

Returns

boolean

withExpiry()

withExpiry(\DateTime|\DateTimeImmutable  $dateTime) : static

Create a cookie with an updated expiration date

Parameters

\DateTime|\DateTimeImmutable $dateTime

Date time object

Returns

static

getExpiry()

getExpiry() : \DateTime|\DateTimeImmutable|null

Get the current expiry time

Returns

\DateTime|\DateTimeImmutable|null —

Timestamp of expiry or null

getExpiresTimestamp()

getExpiresTimestamp() : string|null

Get the timestamp from the expiration time

Timestamps are strings as large timestamps can overflow MAX_INT in 32bit systems.

Returns

string|null —

The expiry time as a string timestamp.

getFormattedExpires()

getFormattedExpires() : string

Builds the expiration value part of the header string

Returns

string

isExpired()

isExpired(\DateTime|\DateTimeImmutable  $time = null) : boolean

Check if a cookie is expired when compared to $time

Cookies without an expiration date always return false.

Parameters

\DateTime|\DateTimeImmutable $time

The time to test against. Defaults to 'now' in UTC.

Returns

boolean

withNeverExpire()

withNeverExpire() : static

Create a new cookie that will virtually never expire.

Returns

static

withExpired()

withExpired() : static

Create a new cookie that will expire/delete the cookie from the browser.

This is done by setting the expiration time to 1 year ago

Returns

static

check()

check(string  $path) : boolean

Checks if a value exists in the cookie data.

This method will expand serialized complex data, on first use.

Parameters

string $path

Path to check

Returns

boolean

withAddedValue()

withAddedValue(string  $path, mixed  $value) : static

Create a new cookie with updated data.

Parameters

string $path

Path to write to

mixed $value

Value to write

Returns

static

withoutAddedValue()

withoutAddedValue(string  $path) : static

Create a new cookie without a specific path

Parameters

string $path

Path to remove

Returns

static

read()

read(string  $path = null) : mixed

Read data from the cookie

This method will expand serialized complex data, on first use.

Parameters

string $path

Path to read the data from

Returns

mixed

isExpanded()

isExpanded() : boolean

Checks if the cookie value was expanded

Returns

boolean

validateName()

validateName(string  $name) : void

Validates the cookie name

Parameters

string $name

Name of the cookie

Throws

\InvalidArgumentException

_setValue()

_setValue(mixed  $value) : void

Setter for the value attribute.

Parameters

mixed $value

The value to store.

validateString()

validateString(string  $value) : void

Validate that an argument is a string

Parameters

string $value

The value to validate.

Throws

\InvalidArgumentException

validateBool()

validateBool(boolean  $value) : void

Validate that an argument is a boolean

Parameters

boolean $value

The value to validate.

Throws

\InvalidArgumentException

_flatten()

_flatten(array  $array) : string

Implode method to keep keys are multidimensional arrays

Parameters

array $array

Map of key and values

Returns

string —

A json encoded string.

_expand()

_expand(string  $string) : string|array

Explode method to return array from string set in CookieComponent::_flatten() Maintains reading backwards compatibility with 1.x CookieComponent::_flatten().

Parameters

string $string

A string containing JSON encoded data, or a bare string.

Returns

string|array —

Map of key and values