CakePHP
  • Documentation
    • Book
    • API
    • Videos
    • Logos & Trademarks
  • Business Solutions
  • Swag
  • Road Trip
  • Team
  • Community
    • Community
    • Team
    • Issues (Github)
    • YouTube Channel
    • Get Involved
    • Bakery
    • Featured Resources
    • Newsletter
    • Certification
    • My CakePHP
    • CakeFest
    • Facebook
    • Twitter
    • Help & Support
    • Forum
    • Stack Overflow
    • IRC
    • Slack
    • Paid Support
CakePHP

C CakePHP 3.8 Red Velvet API

  • Overview
  • Tree
  • Deprecated
  • Version:
    • 3.8
      • 3.8
      • 3.7
      • 3.6
      • 3.5
      • 3.4
      • 3.3
      • 3.2
      • 3.1
      • 3.0
      • 2.10
      • 2.9
      • 2.8
      • 2.7
      • 2.6
      • 2.5
      • 2.4
      • 2.3
      • 2.2
      • 2.1
      • 2.0
      • 1.3
      • 1.2

Namespaces

  • Cake
    • Auth
      • Storage
    • Cache
      • Engine
    • Collection
      • Iterator
    • Command
    • Console
      • Exception
    • Controller
      • Component
      • Exception
    • Core
      • Configure
        • Engine
      • Exception
      • Retry
    • Database
      • Driver
      • Exception
      • Expression
      • Schema
      • Statement
      • Type
    • Datasource
      • Exception
    • Error
      • Middleware
    • Event
      • Decorator
    • Filesystem
    • Form
    • Http
      • Client
        • Adapter
        • Auth
      • Cookie
      • Exception
      • Middleware
      • Session
    • I18n
      • Formatter
      • Middleware
      • Parser
    • Log
      • Engine
    • Mailer
      • Exception
      • Transport
    • Network
      • Exception
    • ORM
      • Association
      • Behavior
        • Translate
      • Exception
      • Locator
      • Rule
    • Routing
      • Exception
      • Filter
      • Middleware
      • Route
    • Shell
      • Helper
      • Task
    • TestSuite
      • Fixture
      • Stub
    • Utility
      • Exception
    • Validation
    • View
      • Exception
      • Form
      • Helper
      • Widget
  • None

Classes

  • Cookie
  • CookieCollection

Interfaces

  • CookieInterface

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

  • EXPIRES_FORMAT

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

  • __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
http://php.net/manual/en/function.setcookie.php

_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
string|array
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
string
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
boolean

getDomain() public ¶

getDomain( )

Get the domain attribute.

Returns
string
Implementation of
Cake\Http\Cookie\CookieInterface::getDomain()

getExpiresTimestamp() 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() public ¶

getExpiry( )

Get the current expiry time

Returns
DateTime|DateTimeImmutable|null
Timestamp of expiry or null
Implementation of
Cake\Http\Cookie\CookieInterface::getExpiry()

getFormattedExpires() public ¶

getFormattedExpires( )

Builds the expiration value part of the header string

Returns
string
Implementation of
Cake\Http\Cookie\CookieInterface::getFormattedExpires()

getId() public ¶

getId( )

Get the id for a cookie

Returns
string
Implementation of
Cake\Http\Cookie\CookieInterface::getId()

getName() public ¶

getName( )

Gets the cookie name

Returns
string
Implementation of
Cake\Http\Cookie\CookieInterface::getName()

getPath() public ¶

getPath( )

Get the path attribute.

Returns
string
Implementation of
Cake\Http\Cookie\CookieInterface::getPath()

getStringValue() public ¶

getStringValue( )

Gets the cookie value as a string.

Returns
string
Implementation of
Cake\Http\Cookie\CookieInterface::getStringValue()

getValue() public ¶

getValue( )

Gets the cookie value

Returns
string|array
Implementation of
Cake\Http\Cookie\CookieInterface::getValue()

isExpanded() public ¶

isExpanded( )

Checks if the cookie value was expanded

Returns
boolean

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
boolean
Implementation of
Cake\Http\Cookie\CookieInterface::isExpired()

isHttpOnly() public ¶

isHttpOnly( )

Check if the cookie is HTTP only

Returns
boolean
Implementation of
Cake\Http\Cookie\CookieInterface::isHttpOnly()

isSecure() public ¶

isSecure( )

Check if the cookie is secure

Returns
boolean
Implementation of
Cake\Http\Cookie\CookieInterface::isSecure()

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
mixed

toHeaderValue() public ¶

toHeaderValue( )

Returns a header value as string

Returns
string
Implementation of
Cake\Http\Cookie\CookieInterface::toHeaderValue()

validateBool() protected ¶

validateBool( boolean $value )

Validate that an argument is a boolean

Parameters
boolean $value
The value to validate.
Throws
InvalidArgumentException

validateName() protected ¶

validateName( string $name )

Validates the cookie name

Parameters
string $name
Name of the cookie
Throws
InvalidArgumentException
Link
Rules for naming cookies.

validateString() protected ¶

validateString( string $value )

Validate that an argument is a string

Parameters
string $value
The value to validate.
Throws
InvalidArgumentException

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
Cake\Http\Cookie\Cookie

withDomain() 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() 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() 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() 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() 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() public ¶

withNeverExpire( )

Create a new cookie that will virtually never expire.

Returns
Cake\Http\Cookie\Cookie
Implementation of
Cake\Http\Cookie\CookieInterface::withNeverExpire()

withPath() 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() 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() 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() 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

$domain ¶

protected string

Domain

''

$expiresAt ¶

protected DateTime|DateTimeImmutable|null

Expiration time

$httpOnly ¶

protected boolean

HTTP only

false

$isExpanded ¶

protected boolean

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

false

$name ¶

protected string

Cookie name

''

$path ¶

protected string

Path

'/'

$secure ¶

protected boolean

Secure

false

$value ¶

protected string|array

Raw Cookie value.

''
Follow @CakePHP
#IRC
OpenHub
Rackspace
  • Business Solutions
  • Showcase
  • Documentation
  • Book
  • API
  • Videos
  • Logos & Trademarks
  • Community
  • Team
  • Issues (Github)
  • YouTube Channel
  • Get Involved
  • Bakery
  • Featured Resources
  • Newsletter
  • Certification
  • My CakePHP
  • CakeFest
  • Facebook
  • Twitter
  • Help & Support
  • Forum
  • Stack Overflow
  • IRC
  • Slack
  • Paid Support

Generated using CakePHP API Docs