On this page
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
-
$expiresAt
protectedDateTime|DateTimeImmutable|null
-
$httpOnly
protectedboolean
-
$isExpanded
protectedboolean
-
$name
protectedstring
-
$path
protectedstring
-
$secure
protectedboolean
-
$value
protectedstring|array
Method Summary
-
__construct() publicConstructor
-
_expand() protected
Explode method to return array from string set in CookieComponent::_flatten() Maintains reading backwards compatibility with 1.x CookieComponent::_flatten().
-
_flatten() protectedImplode method to keep keys are multidimensional arrays
-
_setValue() protectedSetter for the value attribute.
-
check() publicChecks if a value exists in the cookie data.
-
getDomain() publicGet the domain attribute.
-
getExpiresTimestamp() publicGet the timestamp from the expiration time
-
getExpiry() publicGet the current expiry time
-
getFormattedExpires() publicBuilds the expiration value part of the header string
-
getId() publicGet the id for a cookie
-
getName() publicGets the cookie name
-
getPath() publicGet the path attribute.
-
getValue() publicGets the cookie value
-
isExpanded() publicChecks if the cookie value was expanded
-
isExpired() publicCheck if a cookie is expired when compared to $time
-
isHttpOnly() publicCheck if the cookie is HTTP only
-
isSecure() publicCheck if the cookie is secure
-
read() publicRead data from the cookie
-
toHeaderValue() publicReturns a header value as string
-
validateBool() protectedValidate that an argument is a boolean
-
validateName() protectedValidates the cookie name
-
validateString() protectedValidate that an argument is a string
-
withAddedValue() publicCreate a new cookie with updated data.
-
withDomain() publicCreate a cookie with an updated domain
-
withExpired() publicCreate a new cookie that will expire/delete the cookie from the browser.
-
withExpiry() publicCreate a cookie with an updated expiration date
-
withHttpOnly() publicCreate a cookie with HTTP Only updated
-
withName() publicCreate a cookie with an updated name
-
withNeverExpire() publicCreate a new cookie that will virtually never expire.
-
withPath() publicCreate a new cookie with an updated path
-
withSecure() publicCreate a cookie with Secure updated
-
withValue() publicCreate a cookie with an updated value.
-
withoutAddedValue() publicCreate a new cookie without a specific path
Method Detail
__construct()source 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
http://php.net/manual/en/function.setcookie.php_expand()source 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
string|arrayMap of key and values
_flatten()source protected
_flatten( array $array )
Implode method to keep keys are multidimensional arrays
Parameters
-
array
$array
- Map of key and values
Returns
stringA json encoded string.
_setValue()source protected
_setValue( mixed $value )
Setter for the value attribute.
Parameters
-
mixed
$value
- The value to store.
check()source 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
booleangetDomain()source public
getDomain( )
Get the domain attribute.
Returns
stringImplementation of
Cake\Http\Cookie\CookieInterface::getDomain()
getExpiresTimestamp()source public
getExpiresTimestamp( )
Get the timestamp from the expiration time
Timestamps are strings as large timestamps can overflow MAX_INT in 32bit systems.
Returns
string|nullThe expiry time as a string timestamp.
Implementation of
Cake\Http\Cookie\CookieInterface::getExpiresTimestamp()
getExpiry()source public
getExpiry( )
Get the current expiry time
Returns
DateTime|DateTimeImmutable|nullTimestamp of expiry or null
Implementation of
Cake\Http\Cookie\CookieInterface::getExpiry()
getFormattedExpires()source public
getFormattedExpires( )
Builds the expiration value part of the header string
Returns
stringImplementation of
Cake\Http\Cookie\CookieInterface::getFormattedExpires()
getId()source public
getId( )
Get the id for a cookie
Cookies are unique across name, domain, path tuples.
Returns
stringImplementation of
Cake\Http\Cookie\CookieInterface::getId()
getName()source public
getName( )
Gets the cookie name
Returns
stringImplementation of
Cake\Http\Cookie\CookieInterface::getName()
getPath()source public
getPath( )
Get the path attribute.
Returns
stringImplementation of
Cake\Http\Cookie\CookieInterface::getPath()
getValue()source public
getValue( )
Gets the cookie value
Returns
string|arrayImplementation of
Cake\Http\Cookie\CookieInterface::getValue()
isExpired()source public
isExpired( DateTime|DateTimeImmutable $time null )
Check if a cookie is expired when compared to $time
Cookies without an expiration date always return false.
Parameters
-
DateTime|DateTimeImmutable
$time
optional null - The time to test against. Defaults to 'now' in UTC.
Returns
booleanImplementation of
Cake\Http\Cookie\CookieInterface::isExpired()
isHttpOnly()source public
isHttpOnly( )
Check if the cookie is HTTP only
Returns
booleanImplementation of
Cake\Http\Cookie\CookieInterface::isHttpOnly()
isSecure()source public
isSecure( )
Check if the cookie is secure
Returns
booleanImplementation of
Cake\Http\Cookie\CookieInterface::isSecure()
read()source 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
mixedtoHeaderValue()source public
toHeaderValue( )
Returns a header value as string
Returns
stringImplementation of
Cake\Http\Cookie\CookieInterface::toHeaderValue()
validateBool()source protected
validateBool( boolean $value )
Validate that an argument is a boolean
Parameters
-
boolean
$value
- The value to validate.
Throws
InvalidArgumentExceptionvalidateName()source protected
validateName( string $name )
Validates the cookie name
Parameters
-
string
$name
- Name of the cookie
Throws
InvalidArgumentExceptionLink
Rules for naming cookies.validateString()source protected
validateString( string $value )
Validate that an argument is a string
Parameters
-
string
$value
- The value to validate.
Throws
InvalidArgumentExceptionwithAddedValue()source 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
Cake\Http\Cookie\Cookie
withDomain()source public
withDomain( string $domain )
Create a cookie with an updated domain
Parameters
-
string
$domain
- Domain to set
Returns
Cake\Http\Cookie\Cookie
Implementation of
Cake\Http\Cookie\CookieInterface::withDomain()
withExpired()source public
withExpired( )
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
Cake\Http\Cookie\Cookie
Implementation of
Cake\Http\Cookie\CookieInterface::withExpired()
withExpiry()source public
withExpiry( DateTime|DateTimeImmutable $dateTime )
Create a cookie with an updated expiration date
Parameters
-
DateTime|DateTimeImmutable
$dateTime
- Date time object
Returns
Cake\Http\Cookie\Cookie
Implementation of
Cake\Http\Cookie\CookieInterface::withExpiry()
withHttpOnly()source public
withHttpOnly( boolean $httpOnly )
Create a cookie with HTTP Only updated
Parameters
-
boolean
$httpOnly
- HTTP Only
Returns
Cake\Http\Cookie\Cookie
Implementation of
Cake\Http\Cookie\CookieInterface::withHttpOnly()
withName()source public
withName( string $name )
Create a cookie with an updated name
Parameters
-
string
$name
- Name of the cookie
Returns
Cake\Http\Cookie\Cookie
Implementation of
Cake\Http\Cookie\CookieInterface::withName()
withNeverExpire()source public
withNeverExpire( )
Create a new cookie that will virtually never expire.
Returns
Cake\Http\Cookie\Cookie
Implementation of
Cake\Http\Cookie\CookieInterface::withNeverExpire()
withPath()source public
withPath( string $path )
Create a new cookie with an updated path
Parameters
-
string
$path
- Sets the path
Returns
Cake\Http\Cookie\Cookie
Implementation of
Cake\Http\Cookie\CookieInterface::withPath()
withSecure()source public
withSecure( boolean $secure )
Create a cookie with Secure updated
Parameters
-
boolean
$secure
- Secure attribute value
Returns
Cake\Http\Cookie\Cookie
Implementation of
Cake\Http\Cookie\CookieInterface::withSecure()
withValue()source public
withValue( string|array $value )
Create a cookie with an updated value.
Parameters
-
string|array
$value
- Value of the cookie to set
Returns
Cake\Http\Cookie\Cookie
Implementation of
Cake\Http\Cookie\CookieInterface::withValue()
withoutAddedValue()source public
withoutAddedValue( string $path )
Create a new cookie without a specific path
Parameters
-
string
$path
- Path to remove
Returns
Cake\Http\Cookie\Cookie
Properties detail
$isExpandedsource
protected boolean
Whether or not a JSON value has been expanded into an array.
false
© 2005–2017 The Cake Software Foundation, Inc.
Licensed under the MIT License.
CakePHP is a registered trademark of Cake Software Foundation, Inc.
We are not endorsed by or affiliated with CakePHP.
https://api.cakephp.org/3.4/class-Cake.Http.Cookie.Cookie.html