Class Cookie
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');
- Cake\Http\Cookie\Cookie implements Cake\Http\Cookie\CookieInterface
Link: https://tools.ietf.org/html/rfc6265
Link: https://en.wikipedia.org/wiki/HTTP_cookie
See: Cake\Http\Cookie\CookieCollection for working with collections of cookies.
See: Cake\Http\Response::getCookieCollection() for working with response cookies.
Location: Http/Cookie/Cookie.php
Inherited Constants
Properties summary
-
$domain
protectedstring
Domain -
$expiresAt
protectedDateTime|DateTimeImmutable|null
Expiration time -
$httpOnly
protectedboolean
HTTP only -
$isExpanded
protectedboolean
Whether or not a JSON value has been expanded into an array. -
$name
protectedstring
Cookie name -
$path
protectedstring
Path -
$secure
protectedboolean
Secure -
$value
protectedstring|array
Raw Cookie value.
Method Summary
-
__construct() public
Constructor -
_expand() protected
Explode method to return array from string set in CookieComponent::_flatten() Maintains reading backwards compatibility with 1.x CookieComponent::_flatten().
-
_flatten() protected
Implode method to keep keys are multidimensional arrays -
_setValue() protected
Setter for the value attribute. -
check() public
Checks if a value exists in the cookie data. -
getDomain() public
Get the domain attribute. -
getExpiresTimestamp() public
Get the timestamp from the expiration time -
getExpiry() public
Get the current expiry time -
getFormattedExpires() public
Builds the expiration value part of the header string -
getId() public
Get the id for a cookie -
getName() public
Gets the cookie name -
getPath() public
Get the path attribute. -
getStringValue() public
Gets the cookie value as a string. -
getValue() public
Gets the cookie value -
isExpanded() public
Checks if the cookie value was expanded -
isExpired() public
Check if a cookie is expired when compared to $time -
isHttpOnly() public
Check if the cookie is HTTP only -
isSecure() public
Check if the cookie is secure -
read() public
Read data from the cookie -
toHeaderValue() public
Returns a header value as string -
validateBool() protected
Validate that an argument is a boolean -
validateName() protected
Validates the cookie name -
validateString() protected
Validate that an argument is a string -
withAddedValue() public
Create a new cookie with updated data. -
withDomain() public
Create a cookie with an updated domain -
withExpired() public
Create a new cookie that will expire/delete the cookie from the browser. -
withExpiry() public
Create a cookie with an updated expiration date -
withHttpOnly() public
Create a cookie with HTTP Only updated -
withName() public
Sets the cookie name -
withNeverExpire() public
Create a new cookie that will virtually never expire. -
withPath() public
Create a new cookie with an updated path -
withSecure() public
Create a cookie with Secure updated -
withValue() public
Create a cookie with an updated value. -
withoutAddedValue() public
Create a new cookie without a specific path
Method Detail
__construct() public ¶
__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 optional ''
- Value of the cookie
- DateTime|DateTimeImmutable|null $expiresAt optional null
- Expiration time and date
- string $path optional '/'
- Path
- string $domain optional ''
- Domain
- boolean $secure optional false
- Is secure
- boolean $httpOnly optional false
- HTTP Only
Link
_expand() protected ¶
_expand( string $string )
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
Map of key and values
_flatten() protected ¶
_flatten( array $array )
Implode method to keep keys are multidimensional arrays
Parameters
- array $array
- Map of key and values
Returns
A json encoded string.
_setValue() protected ¶
_setValue( mixed $value )
Setter for the value attribute.
Parameters
- mixed $value
- The value to store.
check() public ¶
check( string $path )
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
getExpiresTimestamp() public ¶
getExpiresTimestamp( )
Get the timestamp from the expiration time
Returns
The expiry time as a string timestamp.
Implementation of
getExpiry() public ¶
getExpiry( )
Get the current expiry time
Returns
Timestamp of expiry or null
Implementation of
getFormattedExpires() public ¶
getFormattedExpires( )
Builds the expiration value part of the header string
Returns
Implementation of
getStringValue() public ¶
getStringValue( )
Gets the cookie value as a string.
Returns
Implementation of
isExpired() public ¶
isExpired( DateTime|DateTimeImmutable $time = null )
Check if a cookie is expired when compared to $time
Parameters
- DateTime|DateTimeImmutable $time optional null
- The time to test against. Defaults to 'now' in UTC.
Returns
Implementation of
isHttpOnly() public ¶
isHttpOnly( )
Check if the cookie is HTTP only
Returns
Implementation of
read() public ¶
read( string $path = null )
Read data from the cookie
This method will expand serialized complex data, on first use.
Parameters
- string $path optional null
- Path to read the data from
Returns
toHeaderValue() public ¶
toHeaderValue( )
Returns a header value as string
Returns
Implementation of
validateBool() protected ¶
validateBool( boolean $value )
Validate that an argument is a boolean
Parameters
- boolean $value
- The value to validate.
Throws
validateName() protected ¶
validateName( string $name )
Validates the cookie name
Parameters
- string $name
- Name of the cookie
Throws
Link
validateString() protected ¶
validateString( string $value )
Validate that an argument is a string
Parameters
- string $value
- The value to validate.
Throws
withAddedValue() public ¶
withAddedValue( string $path , mixed $value )
Create a new cookie with updated data.
Parameters
- string $path
- Path to write to
- mixed $value
- Value to write
Returns
withDomain() public ¶
withDomain( string $domain )
Create a cookie with an updated domain
Parameters
- string $domain
- Domain to set
Returns
Implementation of
withExpired() public ¶
withExpired( )
Create a new cookie that will expire/delete the cookie from the browser.
Returns
Implementation of
withExpiry() public ¶
withExpiry( DateTime|DateTimeImmutable $dateTime )
Create a cookie with an updated expiration date
Parameters
- DateTime|DateTimeImmutable $dateTime
- Date time object
Returns
Implementation of
withHttpOnly() public ¶
withHttpOnly( boolean $httpOnly )
Create a cookie with HTTP Only updated
Parameters
- boolean $httpOnly
- HTTP Only
Returns
Implementation of
withName() public ¶
withName( string $name )
Sets the cookie name
Parameters
- string $name
- Name of the cookie
Returns
Implementation of
withNeverExpire() public ¶
withNeverExpire( )
Create a new cookie that will virtually never expire.
Returns
Implementation of
withPath() public ¶
withPath( string $path )
Create a new cookie with an updated path
Parameters
- string $path
- Sets the path
Returns
Implementation of
withSecure() public ¶
withSecure( boolean $secure )
Create a cookie with Secure updated
Parameters
- boolean $secure
- Secure attribute value
Returns
Implementation of
withValue() public ¶
withValue( string|array $value )
Create a cookie with an updated value.
Parameters
- string|array $value
- Value of the cookie to set
Returns
Implementation of
withoutAddedValue() public ¶
withoutAddedValue( string $path )
Create a new cookie without a specific path
Parameters
- string $path
- Path to remove