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
Namespace: Cake\Http\Cookie
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 protected
    string
    Domain
  • $expiresAt protected
    DateTime|DateTimeImmutable|null
    Expiration time
  • $httpOnly protected
    boolean
    HTTP only
  • $isExpanded protected
    boolean
    Whether or not a JSON value has been expanded into an array.
  • $name protected
    string
    Cookie name
  • $path protected
    string
    Path
  • $secure protected
    boolean
    Secure
  • $value protected
    string|array
    Raw Cookie value.

Method Summary

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|array
Map 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

string
A 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

boolean

getDomain()source public

getDomain( )

Get the domain attribute.

Returns

string

Implementation of

Cake\Http\Cookie\CookieInterface::getDomain()

getExpiresTimestamp()source public

getExpiresTimestamp( )

Get the timestamp from the expiration time

Returns

string|null
The 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|null
Timestamp 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

string

Implementation of

Cake\Http\Cookie\CookieInterface::getFormattedExpires()

getId()source public

getId( )

Get the id for a cookie

Returns

string

Implementation of

Cake\Http\Cookie\CookieInterface::getId()

getName()source public

getName( )

Gets the cookie name

Returns

string

Implementation of

Cake\Http\Cookie\CookieInterface::getName()

getPath()source public

getPath( )

Get the path attribute.

Returns

string

Implementation of

Cake\Http\Cookie\CookieInterface::getPath()

getStringValue()source public

getStringValue( )

Gets the cookie value as a string.

Returns

string

Implementation of

Cake\Http\Cookie\CookieInterface::getStringValue()

getValue()source public

getValue( )

Gets the cookie value

Returns

string|array

Implementation of

Cake\Http\Cookie\CookieInterface::getValue()

isExpanded()source public

isExpanded( )

Checks if the cookie value was expanded

Returns

boolean

isExpired()source 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

boolean

Implementation of

Cake\Http\Cookie\CookieInterface::isExpired()

isHttpOnly()source public

isHttpOnly( )

Check if the cookie is HTTP only

Returns

boolean

Implementation of

Cake\Http\Cookie\CookieInterface::isHttpOnly()

isSecure()source public

isSecure( )

Check if the cookie is secure

Returns

boolean

Implementation 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

mixed

toHeaderValue()source public

toHeaderValue( )

Returns a header value as string

Returns

string

Implementation 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

InvalidArgumentException

validateName()source protected

validateName( string $name )

Validates the cookie name

Parameters

string $name
Name of the cookie

Throws

InvalidArgumentException

Link

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

InvalidArgumentException

withAddedValue()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.

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 )

Sets the cookie 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

$domainsource

protected string

Domain

''

$expiresAtsource

protected DateTime|DateTimeImmutable|null

Expiration time

$httpOnlysource

protected boolean

HTTP only

false

$isExpandedsource

protected boolean

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

false

$namesource

protected string

Cookie name

''

$pathsource

protected string

Path

'/'

$securesource

protected boolean

Secure

false

$valuesource

protected string|array

Raw Cookie value.

''

© 2005–present 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.8/class-Cake.Http.Cookie.Cookie.html