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
{@inheritDoc}
fromHeaderString()  : static
Create a new Cookie instance from a `Set-Cookie` header.
getDomain()  : string
{@inheritDoc}
getExpiresString()  : string
{@inheritDoc}
getExpiresTimestamp()  : int
{@inheritDoc}
getId()  : string
{@inheritDoc}
getMaxAge()  : int
{@inheritDoc}
getName()  : string
{@inheritDoc}
getOptions()  : array<string|int, mixed>
{@inheritDoc}
getPath()  : string
{@inheritDoc}
getPrefix()  : string
{@inheritDoc}
getPrefixedName()  : string
{@inheritDoc}
getSameSite()  : string
{@inheritDoc}
getValue()  : string
{@inheritDoc}
isExpired()  : bool
{@inheritDoc}
isHTTPOnly()  : bool
{@inheritDoc}
isRaw()  : bool
{@inheritDoc}
isSecure()  : bool
{@inheritDoc}
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|int, mixed>
{@inheritDoc}
toHeaderString()  : string
{@inheritDoc}
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()

{@inheritDoc}

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

{@inheritDoc}

public getDomain() : string
Return values
string

getExpiresString()

{@inheritDoc}

public getExpiresString() : string
Return values
string

getExpiresTimestamp()

{@inheritDoc}

public getExpiresTimestamp() : int
Return values
int

getId()

{@inheritDoc}

public getId() : string
Return values
string

getMaxAge()

{@inheritDoc}

public getMaxAge() : int
Return values
int

getName()

{@inheritDoc}

public getName() : string
Return values
string

getOptions()

{@inheritDoc}

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

getPath()

{@inheritDoc}

public getPath() : string
Return values
string

getPrefix()

{@inheritDoc}

public getPrefix() : string
Return values
string

getPrefixedName()

{@inheritDoc}

public getPrefixedName() : string
Return values
string

getSameSite()

{@inheritDoc}

public getSameSite() : string
Return values
string

getValue()

{@inheritDoc}

public getValue() : string
Return values
string

isExpired()

{@inheritDoc}

public isExpired() : bool
Return values
bool

isHTTPOnly()

{@inheritDoc}

public isHTTPOnly() : bool
Return values
bool

isRaw()

{@inheritDoc}

public isRaw() : bool
Return values
bool

isSecure()

{@inheritDoc}

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

{@inheritDoc}

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

toHeaderString()

{@inheritDoc}

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
Tags
deprecated

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