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
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
$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
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
boolcreate()
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
Return values
staticcreateFromHeaderString()
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
Return values
staticgetDomain()
Get the domain attribute.
public
getDomain() : string
Tags
Return values
stringgetExpiresTimestamp()
Get the timestamp from the expiration time
public
getExpiresTimestamp() : int|null
Tags
Return values
int|null —The expiry time as an integer.
getExpiry()
Get the current expiry time
public
getExpiry() : DateTime|DateTimeImmutable|null
Tags
Return values
DateTime|DateTimeImmutable|null —Timestamp of expiry or null
getFormattedExpires()
Builds the expiration value part of the header string
public
getFormattedExpires() : string
Tags
Return values
stringgetId()
Get the id for a cookie
public
getId() : string
Tags
Return values
stringgetName()
Gets the cookie name
public
getName() : string
Tags
Return values
stringgetOptions()
Get cookie options
public
getOptions() : array<string, mixed>
Tags
Return values
array<string, mixed>getPath()
Get the path attribute.
public
getPath() : string
Tags
Return values
stringgetSameSite()
Get the SameSite attribute.
public
getSameSite() : string|null
Tags
Return values
string|nullgetScalarValue()
Gets the cookie value as scalar.
public
getScalarValue() : mixed
Tags
getStringValue()
Gets the cookie value as a string.
public
getStringValue() : mixed
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
Return values
array<string|int, mixed>|stringisExpanded()
Checks if the cookie value was expanded
public
isExpanded() : bool
Return values
boolisExpired()
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
Return values
boolisHttpOnly()
Check if the cookie is HTTP only
public
isHttpOnly() : bool
Tags
Return values
boolisSecure()
Check if the cookie is secure
public
isSecure() : bool
Tags
Return values
boolread()
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 orstrtotime()
compatible string orDateTimeInterface
instance ornull
. -
path
: A path string. Defauts to'/'
. -
domain
: Domain name string. Defaults to''
. -
httponly
: Boolean. Defaults tofalse
. -
secure
: Boolean. Defaults tofalse
. -
samesite
: Can be one ofCookieInterface::SAMESITE_LAX
,CookieInterface::SAMESITE_STRICT
,CookieInterface::SAMESITE_NONE
ornull
. Defaults tonull
.
Parameters
- $options : array<string, mixed>
-
Default options.
toArray()
Get cookie data as array.
public
toArray() : array<string, mixed>
Tags
Return values
array<string, mixed> —With keys name
, value
, expires
etc. options.
toHeaderValue()
Returns a header value as string
public
toHeaderValue() : string
Return values
stringwithAddedValue()
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
staticwithDomain()
Create a cookie with an updated domain
public
withDomain(string $domain) : static
Parameters
- $domain : string
-
Domain to set
Tags
Return values
staticwithExpired()
Create a new cookie that will expire/delete the cookie from the browser.
public
withExpired() : static
Tags
Return values
staticwithExpiry()
Create a cookie with an updated expiration date
public
withExpiry(mixed $dateTime) : static
Parameters
- $dateTime : mixed
-
Date time object
Tags
Return values
staticwithHttpOnly()
Create a cookie with HTTP Only updated
public
withHttpOnly(bool $httpOnly) : static
Parameters
- $httpOnly : bool
-
HTTP Only
Tags
Return values
staticwithName()
Sets the cookie name
public
withName(string $name) : static
Parameters
- $name : string
-
Name of the cookie
Tags
Return values
staticwithNeverExpire()
Create a new cookie that will virtually never expire.
public
withNeverExpire() : static
Tags
Return values
staticwithoutAddedValue()
Create a new cookie without a specific path
public
withoutAddedValue(string $path) : static
Parameters
- $path : string
-
Path to remove
Return values
staticwithPath()
Create a new cookie with an updated path
public
withPath(string $path) : static
Parameters
- $path : string
-
Sets the path
Tags
Return values
staticwithSameSite()
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
Return values
staticwithSecure()
Create a cookie with Secure updated
public
withSecure(bool $secure) : static
Parameters
- $secure : bool
-
Secure attribute value
Tags
Return values
staticwithValue()
Create a cookie with an updated value.
public
withValue(mixed $value) : static
Parameters
- $value : mixed
-
Value of the cookie to set
Tags
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|nullvalidateName()
Validates the cookie name
protected
validateName(string $name) : void
Parameters
- $name : string
-
Name of the cookie
Tags
validateSameSiteValue()
Check that value passed for SameSite is valid.
protected
static validateSameSiteValue(string $sameSite) : void
Parameters
- $sameSite : string
-
SameSite value