CodeIgniter v4.5 API

Cookie
in package
implements ArrayAccess, CloneableCookieInterface

A `Cookie` class represents an immutable HTTP cookie value object.

Being immutable, modifying one or more of its attributes will return a new Cookie instance, rather than modifying itself. Users should reassign this new instance to a new variable to capture it.

$cookie = new Cookie('test_cookie', 'test_value');
$cookie->getName(); // test_cookie

$cookie->withName('prod_cookie');
$cookie->getName(); // test_cookie

$cookie2 = $cookie->withName('prod_cookie');
$cookie2->getName(); // prod_cookie
Tags
template-implements

ArrayAccess<string, bool|int|string>

see
CookieTest

Table of Contents

Interfaces

ArrayAccess
CloneableCookieInterface
Interface for a fresh Cookie instance with selected attribute(s) only changed from the original instance.

Properties

$domain  : string
$expires  : int
$httponly  : bool
$name  : string
$path  : string
$prefix  : string
$raw  : bool
$samesite  : string
$secure  : bool
$value  : string
$defaults  : array<string, bool|int|string>
Default attributes for a Cookie object. The keys here are the lowercase attribute names. Do not camelCase!
$reservedCharsList  : string
A cookie name can be any US-ASCII characters, except control characters, spaces, tabs, or separator characters.

Methods

__construct()  : mixed
Construct a new Cookie instance.
__toString()  : string
Returns the string representation of the Cookie object.
fromHeaderString()  : static
Create a new Cookie instance from a `Set-Cookie` header.
getDomain()  : string
Gets the "Domain" cookie attribute.
getExpiresString()  : string
Gets the formatted expires time.
getExpiresTimestamp()  : int
Gets the time in Unix timestamp the cookie expires.
getId()  : string
Returns a unique identifier for the cookie consisting of its prefixed name, path, and domain.
getMaxAge()  : int
Gets the "Max-Age" cookie attribute.
getName()  : string
Gets the cookie name.
getOptions()  : array<string, bool|int|string>
Gets the options that are passable to the `setcookie` variant available on PHP 7.3+
getPath()  : string
Gets the "Path" cookie attribute.
getPrefix()  : string
Gets the cookie prefix.
getPrefixedName()  : string
Gets the cookie name prepended with the prefix, if any.
getSameSite()  : string
Gets the "SameSite" cookie attribute.
getValue()  : string
Gets the cookie value.
isExpired()  : bool
Checks if the cookie is expired.
isHTTPOnly()  : bool
Gets the "HttpOnly" cookie attribute.
isRaw()  : bool
Checks if the cookie should be sent with no URL encoding.
isSecure()  : bool
Gets the "Secure" cookie attribute.
offsetExists()  : bool
Whether an offset exists.
offsetGet()  : bool|int|string
Offset to retrieve.
offsetSet()  : void
Offset to set.
offsetUnset()  : void
Offset to unset.
setDefaults()  : array<string, mixed>
Set the default attributes to a Cookie instance by injecting the values from the `CookieConfig` config or an array.
toArray()  : array<string, bool|int|string>
Returns the array representation of the Cookie object.
toHeaderString()  : string
Returns the Cookie as a header value.
withDomain()  : static
Creates a new Cookie with a new domain the cookie is available.
withExpired()  : static
Creates a new Cookie that will expire the cookie from the browser.
withExpires()  : static
Creates a new Cookie with a new cookie expires time.
withHTTPOnly()  : static
Creates a new Cookie with a new "HttpOnly" attribute
withName()  : static
Creates a new Cookie with a new name.
withNeverExpiring()  : static
Creates a new Cookie that will virtually never expire from the browser.
withPath()  : static
Creates a new Cookie with a new path on the server the cookie is available.
withPrefix()  : static
Creates a new Cookie with a new cookie prefix.
withRaw()  : static
Creates a new Cookie with URL encoding option updated.
withSameSite()  : static
Creates a new Cookie with a new "SameSite" attribute.
withSecure()  : static
Creates a new Cookie with a new "Secure" attribute.
withValue()  : static
Creates a new Cookie with new value.
convertExpiresTimestamp()  : int
Converts expires time to Unix format.
validateName()  : void
Validates the cookie name per RFC 2616.
validatePrefix()  : void
Validates the special prefixes if some attribute requirements are met.
validateSameSite()  : void
Validates the `SameSite` to be within the allowed types.

Properties

$domain

protected string $domain = ''

$expires

protected int $expires

Unix timestamp

$httponly

protected bool $httponly = true

$path

protected string $path = '/'

$prefix

protected string $prefix = ''

$raw

protected bool $raw = false

$samesite

protected string $samesite = self::SAMESITE_LAX

$secure

protected bool $secure = false

$defaults

Default attributes for a Cookie object. The keys here are the lowercase attribute names. Do not camelCase!

private static array<string, bool|int|string> $defaults = ['prefix' => '', 'expires' => 0, 'path' => '/', 'domain' => '', 'secure' => false, 'httponly' => true, 'samesite' => self::SAMESITE_LAX, 'raw' => false]

Methods

__construct()

Construct a new Cookie instance.

public final __construct(string $name[, string $value = '' ][, array<string, bool|int|string> $options = [] ]) : mixed
Parameters
$name : string

The cookie's name

$value : string = ''

The cookie's value

$options : array<string, bool|int|string> = []

The cookie's options

Tags
throws
CookieException

__toString()

Returns the string representation of the Cookie object.

public __toString() : string
Return values
string

fromHeaderString()

Create a new Cookie instance from a `Set-Cookie` header.

public static fromHeaderString(string $cookie[, bool $raw = false ]) : static
Parameters
$cookie : string
$raw : bool = false
Tags
throws
CookieException
Return values
static

getDomain()

Gets the "Domain" cookie attribute.

public getDomain() : string
Return values
string

getExpiresString()

Gets the formatted expires time.

public getExpiresString() : string
Return values
string

getExpiresTimestamp()

Gets the time in Unix timestamp the cookie expires.

public getExpiresTimestamp() : int
Return values
int

getId()

Returns a unique identifier for the cookie consisting of its prefixed name, path, and domain.

public getId() : string
Return values
string

getMaxAge()

Gets the "Max-Age" cookie attribute.

public getMaxAge() : int
Return values
int

getName()

Gets the cookie name.

public getName() : string
Return values
string

getOptions()

Gets the options that are passable to the `setcookie` variant available on PHP 7.3+

public getOptions() : array<string, bool|int|string>
Return values
array<string, bool|int|string>

getPath()

Gets the "Path" cookie attribute.

public getPath() : string
Return values
string

getPrefix()

Gets the cookie prefix.

public getPrefix() : string
Return values
string

getPrefixedName()

Gets the cookie name prepended with the prefix, if any.

public getPrefixedName() : string
Return values
string

getSameSite()

Gets the "SameSite" cookie attribute.

public getSameSite() : string
Return values
string

getValue()

Gets the cookie value.

public getValue() : string
Return values
string

isExpired()

Checks if the cookie is expired.

public isExpired() : bool
Return values
bool

isHTTPOnly()

Gets the "HttpOnly" cookie attribute.

public isHTTPOnly() : bool
Return values
bool

isRaw()

Checks if the cookie should be sent with no URL encoding.

public isRaw() : bool
Return values
bool

isSecure()

Gets the "Secure" cookie attribute.

public isSecure() : bool
Return values
bool

offsetExists()

Whether an offset exists.

public offsetExists(string $offset) : bool
Parameters
$offset : string
Return values
bool

offsetGet()

Offset to retrieve.

public offsetGet(string $offset) : bool|int|string
Parameters
$offset : string
Tags
throws
InvalidArgumentException
Attributes
#[ReturnTypeWillChange]
Return values
bool|int|string

offsetSet()

Offset to set.

public offsetSet(string $offset, bool|int|string $value) : void
Parameters
$offset : string
$value : bool|int|string
Tags
throws
LogicException

offsetUnset()

Offset to unset.

public offsetUnset(string $offset) : void
Parameters
$offset : string
Tags
throws
LogicException

setDefaults()

Set the default attributes to a Cookie instance by injecting the values from the `CookieConfig` config or an array.

public static setDefaults([array<string, bool|int|string>|Cookie $config = [] ]) : array<string, mixed>

This method is called from Response::__construct().

Parameters
$config : array<string, bool|int|string>|Cookie = []
Return values
array<string, mixed>

The old defaults array. Useful for resetting.

toArray()

Returns the array representation of the Cookie object.

public toArray() : array<string, bool|int|string>
Return values
array<string, bool|int|string>

toHeaderString()

Returns the Cookie as a header value.

public toHeaderString() : string
Return values
string

withDomain()

Creates a new Cookie with a new domain the cookie is available.

public withDomain(string|null $domain) : static
Parameters
$domain : string|null
Return values
static

withExpired()

Creates a new Cookie that will expire the cookie from the browser.

public withExpired() : static
Return values
static

withExpires()

Creates a new Cookie with a new cookie expires time.

public withExpires(mixed $expires) : static
Parameters
$expires : mixed
Return values
static

withHTTPOnly()

Creates a new Cookie with a new "HttpOnly" attribute

public withHTTPOnly([bool $httponly = true ]) : static
Parameters
$httponly : bool = true
Return values
static

withName()

Creates a new Cookie with a new name.

public withName(string $name) : static
Parameters
$name : string
Return values
static

withNeverExpiring()

Creates a new Cookie that will virtually never expire from the browser.

public withNeverExpiring() : static

See https://github.com/codeigniter4/CodeIgniter4/pull/6413

Return values
static

withPath()

Creates a new Cookie with a new path on the server the cookie is available.

public withPath(string|null $path) : static
Parameters
$path : string|null
Return values
static

withPrefix()

Creates a new Cookie with a new cookie prefix.

public withPrefix([string $prefix = '' ]) : static
Parameters
$prefix : string = ''
Return values
static

withRaw()

Creates a new Cookie with URL encoding option updated.

public withRaw([bool $raw = true ]) : static
Parameters
$raw : bool = true
Return values
static

withSameSite()

Creates a new Cookie with a new "SameSite" attribute.

public withSameSite(string $samesite) : static
Parameters
$samesite : string
Return values
static

withSecure()

Creates a new Cookie with a new "Secure" attribute.

public withSecure([bool $secure = true ]) : static
Parameters
$secure : bool = true
Return values
static

withValue()

Creates a new Cookie with new value.

public withValue(string $value) : static
Parameters
$value : string
Return values
static

convertExpiresTimestamp()

Converts expires time to Unix format.

protected static convertExpiresTimestamp([DateTimeInterface|int|string $expires = 0 ]) : int
Parameters
$expires : DateTimeInterface|int|string = 0
Return values
int

validateName()

Validates the cookie name per RFC 2616.

protected validateName(string $name, bool $raw) : void

If $raw is true, names should not contain invalid characters as setrawcookie() will reject this.

Parameters
$name : string
$raw : bool
Tags
throws
CookieException

validatePrefix()

Validates the special prefixes if some attribute requirements are met.

protected validatePrefix(string $prefix, bool $secure, string $path, string $domain) : void
Parameters
$prefix : string
$secure : bool
$path : string
$domain : string
Tags
throws
CookieException

        
On this page

Search results