Fr3nch13/CakePHP Utilities

Cookie
in package
implements CookieInterface

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');
Tags
link
https://tools.ietf.org/html/draft-ietf-httpbis-rfc6265bis-03
link
https://en.wikipedia.org/wiki/HTTP_cookie
see
CookieCollection

for working with collections of cookies.

see
Response::getCookieCollection()

for working with response cookies.

Table of Contents

Interfaces

CookieInterface
Cookie Interface

Properties

$defaults  : array<string, mixed>
Default attributes for a cookie.
$domain  : string
Domain
$expiresAt  : DateTime|DateTimeImmutable|null
Expiration time
$httpOnly  : bool
HTTP only
$isExpanded  : bool
Whether a JSON value has been expanded into an array.
$name  : string
Cookie name
$path  : string
Path
$sameSite  : string|null
Samesite
$secure  : bool
Secure
$value  : array<string|int, mixed>|string
Raw Cookie value.

Methods

__construct()  : mixed
Constructor
check()  : bool
Checks if a value exists in the cookie data.
create()  : static
Factory method to create Cookie instances.
createFromHeaderString()  : static
Create Cookie instance from "set-cookie" header string.
getDomain()  : string
Get the domain attribute.
getExpiresTimestamp()  : int|null
Get the timestamp from the expiration time
getExpiry()  : DateTime|DateTimeImmutable|null
Get the current expiry time
getFormattedExpires()  : string
Builds the expiration value part of the header string
getId()  : string
Get the id for a cookie
getName()  : string
Gets the cookie name
getOptions()  : array<string, mixed>
Get cookie options
getPath()  : string
Get the path attribute.
getSameSite()  : string|null
Get the SameSite attribute.
getScalarValue()  : mixed
Gets the cookie value as scalar.
getStringValue()  : mixed
Gets the cookie value as a string.
getValue()  : array<string|int, mixed>|string
Gets the cookie value
isExpanded()  : bool
Checks if the cookie value was expanded
isExpired()  : bool
Check if a cookie is expired when compared to $time
isHttpOnly()  : bool
Check if the cookie is HTTP only
isSecure()  : bool
Check if the cookie is secure
read()  : mixed
Read data from the cookie
setDefaults()  : void
Set default options for the cookies.
toArray()  : array<string, mixed>
Get cookie data as array.
toHeaderValue()  : string
Returns a header value as string
withAddedValue()  : static
Create a new cookie with updated data.
withDomain()  : static
Create a cookie with an updated domain
withExpired()  : static
Create a new cookie that will expire/delete the cookie from the browser.
withExpiry()  : static
Create a cookie with an updated expiration date
withHttpOnly()  : static
Create a cookie with HTTP Only updated
withName()  : static
Sets the cookie name
withNeverExpire()  : static
Create a new cookie that will virtually never expire.
withoutAddedValue()  : static
Create a new cookie without a specific path
withPath()  : static
Create a new cookie with an updated path
withSameSite()  : static
Create a cookie with an updated SameSite option.
withSecure()  : static
Create a cookie with Secure updated
withValue()  : static
Create a cookie with an updated value.
_expand()  : array<string|int, mixed>|string
Explode method to return array from string set in CookieComponent::_flatten() Maintains reading backwards compatibility with 1.x CookieComponent::_flatten().
_flatten()  : string
Implode method to keep keys are multidimensional arrays
_setValue()  : void
Setter for the value attribute.
dateTimeInstance()  : DateTime|DateTimeImmutable|null
Converts non null expiry value into DateTimeInterface instance.
validateName()  : void
Validates the cookie name
validateSameSiteValue()  : void
Check that value passed for SameSite is valid.

Properties

$defaults

Default attributes for a cookie.

protected static array<string, mixed> $defaults = ['expires' => null, 'path' => '/', 'domain' => '', 'secure' => false, 'httponly' => false, 'samesite' => null]
Tags
see
Cookie::setDefaults()

$domain

Domain

protected string $domain = ''

$expiresAt

Expiration time

protected DateTime|DateTimeImmutable|null $expiresAt

$httpOnly

HTTP only

protected bool $httpOnly = false

$isExpanded

Whether a JSON value has been expanded into an array.

protected bool $isExpanded = false

$name

Cookie name

protected string $name = ''

$path

Path

protected string $path = '/'

$sameSite

Samesite

protected string|null $sameSite = null

$secure

Secure

protected bool $secure = false

$value

Raw Cookie value.

protected array<string|int, mixed>|string $value = ''

Methods

__construct()

Constructor

public __construct(string $name[, array<string|int, mixed>|string $value = '' ][, DateTime|DateTimeImmutable|null $expiresAt = null ][, string|null $path = null ][, string|null $domain = null ][, bool|null $secure = null ][, bool|null $httpOnly = null ][, string|null $sameSite = null ]) : mixed

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
$name : string

Cookie name

$value : array<string|int, mixed>|string = ''

Value of the cookie

$expiresAt : DateTime|DateTimeImmutable|null = null

Expiration time and date

$path : string|null = null

Path

$domain : string|null = null

Domain

$secure : bool|null = null

Is secure

$httpOnly : bool|null = null

HTTP Only

$sameSite : string|null = null

Samesite

Tags
link
https://php.net/manual/en/function.setcookie.php

check()

Checks if a value exists in the cookie data.

public check(string $path) : bool

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

Parameters
$path : string

Path to check

Return values
bool

create()

Factory method to create Cookie instances.

public static create(string $name, array<string|int, mixed>|string $value[, array<string, mixed> $options = [] ]) : static
Parameters
$name : string

Cookie name

$value : array<string|int, mixed>|string

Value of the cookie

$options : array<string, mixed> = []

Cookies options.

Tags
see
Cookie::setDefaults()
Return values
static

createFromHeaderString()

Create Cookie instance from "set-cookie" header string.

public static createFromHeaderString(string $cookie[, array<string, mixed> $defaults = [] ]) : static
Parameters
$cookie : string

Cookie header string.

$defaults : array<string, mixed> = []

Default attributes.

Tags
see
Cookie::setDefaults()
Return values
static

getDomain()

Get the domain attribute.

public getDomain() : string
Tags
inheritDoc
Return values
string

getExpiresTimestamp()

Get the timestamp from the expiration time

public getExpiresTimestamp() : int|null
Tags
inheritDoc
Return values
int|null

The expiry time as an integer.

getExpiry()

Get the current expiry time

public getExpiry() : DateTime|DateTimeImmutable|null
Tags
inheritDoc
Return values
DateTime|DateTimeImmutable|null

Timestamp of expiry or null

getFormattedExpires()

Builds the expiration value part of the header string

public getFormattedExpires() : string
Tags
inheritDoc
Return values
string

getId()

Get the id for a cookie

public getId() : string
Tags
inheritDoc
Return values
string

getName()

Gets the cookie name

public getName() : string
Tags
inheritDoc
Return values
string

getOptions()

Get cookie options

public getOptions() : array<string, mixed>
Tags
inheritDoc
Return values
array<string, mixed>

getPath()

Get the path attribute.

public getPath() : string
Tags
inheritDoc
Return values
string

getSameSite()

Get the SameSite attribute.

public getSameSite() : string|null
Tags
inheritDoc
Return values
string|null

getScalarValue()

Gets the cookie value as scalar.

public getScalarValue() : mixed
Tags
inheritDoc

getStringValue()

Gets the cookie value as a string.

public getStringValue() : mixed

Use instead.

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

getValue()

Gets the cookie value

public getValue() : array<string|int, mixed>|string
Tags
inheritDoc
Return values
array<string|int, mixed>|string

isExpanded()

Checks if the cookie value was expanded

public isExpanded() : bool
Return values
bool

isExpired()

Check if a cookie is expired when compared to $time

public isExpired([mixed $time = null ]) : bool
Parameters
$time : mixed = null

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

Tags
inheritDoc
Return values
bool

isHttpOnly()

Check if the cookie is HTTP only

public isHttpOnly() : bool
Tags
inheritDoc
Return values
bool

isSecure()

Check if the cookie is secure

public isSecure() : bool
Tags
inheritDoc
Return values
bool

read()

Read data from the cookie

public read([string|null $path = null ]) : mixed

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

Parameters
$path : string|null = null

Path to read the data from

setDefaults()

Set default options for the cookies.

public static setDefaults(array<string, mixed> $options) : void

Valid option keys are:

  • expires: Can be a UNIX timestamp or strtotime() compatible string or DateTimeInterface instance or null.
  • path: A path string. Defauts to '/'.
  • domain: Domain name string. Defaults to ''.
  • httponly: Boolean. Defaults to false.
  • secure: Boolean. Defaults to false.
  • samesite: Can be one of CookieInterface::SAMESITE_LAX, CookieInterface::SAMESITE_STRICT, CookieInterface::SAMESITE_NONE or null. Defaults to null.
Parameters
$options : array<string, mixed>

Default options.

toArray()

Get cookie data as array.

public toArray() : array<string, mixed>
Tags
inheritDoc
Return values
array<string, mixed>

With keys name, value, expires etc. options.

toHeaderValue()

Returns a header value as string

public toHeaderValue() : string
Return values
string

withAddedValue()

Create a new cookie with updated data.

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

Path to write to

$value : mixed

Value to write

Return values
static

withDomain()

Create a cookie with an updated domain

public withDomain(string $domain) : static
Parameters
$domain : string

Domain to set

Tags
inheritDoc
Return values
static

withExpired()

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

public withExpired() : static
Tags
inheritDoc
Return values
static

withExpiry()

Create a cookie with an updated expiration date

public withExpiry(mixed $dateTime) : static
Parameters
$dateTime : mixed

Date time object

Tags
inheritDoc
Return values
static

withHttpOnly()

Create a cookie with HTTP Only updated

public withHttpOnly(bool $httpOnly) : static
Parameters
$httpOnly : bool

HTTP Only

Tags
inheritDoc
Return values
static

withName()

Sets the cookie name

public withName(string $name) : static
Parameters
$name : string

Name of the cookie

Tags
inheritDoc
Return values
static

withNeverExpire()

Create a new cookie that will virtually never expire.

public withNeverExpire() : static
Tags
inheritDoc
Return values
static

withoutAddedValue()

Create a new cookie without a specific path

public withoutAddedValue(string $path) : static
Parameters
$path : string

Path to remove

Return values
static

withPath()

Create a new cookie with an updated path

public withPath(string $path) : static
Parameters
$path : string

Sets the path

Tags
inheritDoc
Return values
static

withSameSite()

Create a cookie with an updated SameSite option.

public withSameSite(string|null $sameSite) : static
Parameters
$sameSite : string|null

Value for to set for Samesite option. One of CookieInterface::SAMESITE_* constants.

Tags
inheritDoc
Return values
static

withSecure()

Create a cookie with Secure updated

public withSecure(bool $secure) : static
Parameters
$secure : bool

Secure attribute value

Tags
inheritDoc
Return values
static

withValue()

Create a cookie with an updated value.

public withValue(mixed $value) : static
Parameters
$value : mixed

Value of the cookie to set

Tags
inheritDoc
Return values
static

_expand()

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

protected _expand(string $string) : array<string|int, mixed>|string
Parameters
$string : string

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

Return values
array<string|int, mixed>|string

Map of key and values

_flatten()

Implode method to keep keys are multidimensional arrays

protected _flatten(array<string|int, mixed> $array) : string
Parameters
$array : array<string|int, mixed>

Map of key and values

Return values
string

A JSON encoded string.

_setValue()

Setter for the value attribute.

protected _setValue(array<string|int, mixed>|string $value) : void
Parameters
$value : array<string|int, mixed>|string

The value to store.

dateTimeInstance()

Converts non null expiry value into DateTimeInterface instance.

protected static dateTimeInstance(mixed $expires) : DateTime|DateTimeImmutable|null
Parameters
$expires : mixed

Expiry value.

Return values
DateTime|DateTimeImmutable|null

validateName()

Validates the cookie name

protected validateName(string $name) : void
Parameters
$name : string

Name of the cookie

Tags
throws
InvalidArgumentException
link

Rules for naming cookies.

validateSameSiteValue()

Check that value passed for SameSite is valid.

protected static validateSameSiteValue(string $sameSite) : void
Parameters
$sameSite : string

SameSite value

Tags
throws
InvalidArgumentException

        
On this page

Search results