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
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
$name
protected
string
$name
$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
$value
protected
string
$value
$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]
$reservedCharsList
A cookie name can be any US-ASCII characters, except control characters, spaces, tabs, or separator characters.
private
static string
$reservedCharsList
= "=,; \t\r\n\v\f()<>@:\\\"/[]?{}"
Tags
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
__toString()
Returns the string representation of the Cookie object.
public
__toString() : string
Return values
stringfromHeaderString()
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
Return values
staticgetDomain()
Gets the "Domain" cookie attribute.
public
getDomain() : string
Return values
stringgetExpiresString()
Gets the formatted expires time.
public
getExpiresString() : string
Return values
stringgetExpiresTimestamp()
Gets the time in Unix timestamp the cookie expires.
public
getExpiresTimestamp() : int
Return values
intgetId()
Returns a unique identifier for the cookie consisting of its prefixed name, path, and domain.
public
getId() : string
Return values
stringgetMaxAge()
Gets the "Max-Age" cookie attribute.
public
getMaxAge() : int
Return values
intgetName()
Gets the cookie name.
public
getName() : string
Return values
stringgetOptions()
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
stringgetPrefix()
Gets the cookie prefix.
public
getPrefix() : string
Return values
stringgetPrefixedName()
Gets the cookie name prepended with the prefix, if any.
public
getPrefixedName() : string
Return values
stringgetSameSite()
Gets the "SameSite" cookie attribute.
public
getSameSite() : string
Return values
stringgetValue()
Gets the cookie value.
public
getValue() : string
Return values
stringisExpired()
Checks if the cookie is expired.
public
isExpired() : bool
Return values
boolisHTTPOnly()
Gets the "HttpOnly" cookie attribute.
public
isHTTPOnly() : bool
Return values
boolisRaw()
Checks if the cookie should be sent with no URL encoding.
public
isRaw() : bool
Return values
boolisSecure()
Gets the "Secure" cookie attribute.
public
isSecure() : bool
Return values
booloffsetExists()
Whether an offset exists.
public
offsetExists(string $offset) : bool
Parameters
- $offset : string
Return values
booloffsetGet()
Offset to retrieve.
public
offsetGet(string $offset) : bool|int|string
Parameters
- $offset : string
Tags
Attributes
- #[ReturnTypeWillChange]
Return values
bool|int|stringoffsetSet()
Offset to set.
public
offsetSet(string $offset, bool|int|string $value) : void
Parameters
- $offset : string
- $value : bool|int|string
Tags
offsetUnset()
Offset to unset.
public
offsetUnset(string $offset) : void
Parameters
- $offset : string
Tags
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
stringwithDomain()
Creates a new Cookie with a new domain the cookie is available.
public
withDomain(string|null $domain) : static
Parameters
- $domain : string|null
Return values
staticwithExpired()
Creates a new Cookie that will expire the cookie from the browser.
public
withExpired() : static
Return values
staticwithExpires()
Creates a new Cookie with a new cookie expires time.
public
withExpires(mixed $expires) : static
Parameters
- $expires : mixed
Return values
staticwithHTTPOnly()
Creates a new Cookie with a new "HttpOnly" attribute
public
withHTTPOnly([bool $httponly = true ]) : static
Parameters
- $httponly : bool = true
Return values
staticwithName()
Creates a new Cookie with a new name.
public
withName(string $name) : static
Parameters
- $name : string
Return values
staticwithNeverExpiring()
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
staticwithPath()
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
staticwithPrefix()
Creates a new Cookie with a new cookie prefix.
public
withPrefix([string $prefix = '' ]) : static
Parameters
- $prefix : string = ''
Return values
staticwithRaw()
Creates a new Cookie with URL encoding option updated.
public
withRaw([bool $raw = true ]) : static
Parameters
- $raw : bool = true
Return values
staticwithSameSite()
Creates a new Cookie with a new "SameSite" attribute.
public
withSameSite(string $samesite) : static
Parameters
- $samesite : string
Return values
staticwithSecure()
Creates a new Cookie with a new "Secure" attribute.
public
withSecure([bool $secure = true ]) : static
Parameters
- $secure : bool = true
Return values
staticwithValue()
Creates a new Cookie with new value.
public
withValue(string $value) : static
Parameters
- $value : string
Return values
staticconvertExpiresTimestamp()
Converts expires time to Unix format.
protected
static convertExpiresTimestamp([DateTimeInterface|int|string $expires = 0 ]) : int
Parameters
- $expires : DateTimeInterface|int|string = 0
Return values
intvalidateName()
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
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
validateSameSite()
Validates the `SameSite` to be within the allowed types.
protected
validateSameSite(string $samesite, bool $secure) : void
Parameters
- $samesite : string
- $secure : bool