class Cookie (View source)

A class representing a list of cookies with specific attributes.

Constants

PARSE_RAW

Do not decode cookie contents.

SECURE

The cookies' flags have the secure attribute set.

HTTPONLY

The cookies' flags have the httpOnly attribute set.

Methods

__construct(mixed $cookies = null, int $flags = 0, array $allowed_extras = null)

Create a new cookie list.

string
__toString()

String cast handler. Alias of http\Cookie::toString().

addCookie(string $cookie_name, string $cookie_value)

Add a cookie.

addCookies(array $cookies)

(Re)set the cookies.

addExtra(string $extra_name, string $extra_value)

Add an extra attribute to the cookie list.

addExtras(array $extras)

Add several extra attributes.

string|null
getCookie(string $cookie_name)

Retrieve a specific cookie value.

array
getCookies()

Get the list of cookies.

string
getDomain()

Retrieve the effective domain of the cookie list.

int
getExpires()

Get the currently set expires attribute.

string
getExtra(string $name)

Retrieve an extra attribute.

array
getExtras()

Retrieve the list of extra attributes.

int
getFlags()

Get the currently set flags.

int
getMaxAge()

Get the currently set max-age attribute of the cookie list.

string
getPath()

Retrieve the path the cookie(s) of this cookie list are effective at.

setCookie(string $cookie_name, string $cookie_value)

(Re)set a cookie.

setCookies(array $cookies = null)

(Re)set the cookies.

setDomain(string $value = null)

Set the effective domain of the cookie list.

setExpires(int $value = -1)

Set the traditional expires timestamp.

setExtra(string $extra_name, string $extra_value = null)

(Re)set an extra attribute.

setExtras(array $extras = null)

(Re)set the extra attributes.

setFlags(int $value = 0)

Set the flags to specified $value.

setMaxAge(int $value = -1)

Set the maximum age the cookie may have on the client side.

setPath(string $path = null)

Set the path the cookie(s) of this cookie list should be effective at.

array
toArray()

Get the cookie list as array.

string
toString()

Retrieve the string representation of the cookie list.

Details

__construct(mixed $cookies = null, int $flags = 0, array $allowed_extras = null)

Create a new cookie list.

Parameters

mixed $cookies

The string or list of cookies to parse or set.

int $flags

Parse flags. See http\Cookie::PARSE_* constants.

array $allowed_extras

List of extra attribute names to recognize.

Exceptions

InvalidArgumentException
RuntimeException

string __toString()

String cast handler. Alias of http\Cookie::toString().

Return Value

string

the cookie(s) represented as string.

Cookie addCookie(string $cookie_name, string $cookie_value)

Add a cookie.

See http\Cookie::setCookie() and http\Cookie::addCookies().

Parameters

string $cookie_name

The key of the cookie.

string $cookie_value

The value of the cookie.

Return Value

Cookie self.

Exceptions

InvalidArgumentException

Cookie addCookies(array $cookies)

(Re)set the cookies.

See http\Cookie::setCookies().

Parameters

array $cookies

Add cookies of this array of form ["name" => "value"].

Return Value

Cookie self.

Exceptions

InvalidArgumentException

Cookie addExtra(string $extra_name, string $extra_value)

Add an extra attribute to the cookie list.

See http\Cookie::setExtra().

Parameters

string $extra_name

The key of the extra attribute.

string $extra_value

The value of the extra attribute.

Return Value

Cookie self.

Exceptions

InvalidArgumentException

Cookie addExtras(array $extras)

Add several extra attributes.

See http\Cookie::addExtra().

Parameters

array $extras

A list of extra attributes of the form ["key" => "value"].

Return Value

Cookie self.

Exceptions

InvalidArgumentException

string|null getCookie(string $cookie_name)

Retrieve a specific cookie value.

See http\Cookie::setCookie().

Parameters

string $cookie_name

The key of the cookie to look up.

Return Value

string|null

string the cookie value. or NULL if $cookie_name could not be found.

array getCookies()

Get the list of cookies.

See http\Cookie::setCookies().

Return Value

array

the list of cookies of form ["name" => "value"].

string getDomain()

Retrieve the effective domain of the cookie list.

See http\Cookie::setDomain().

Return Value

string

the effective domain.

int getExpires()

Get the currently set expires attribute.

See http\Cookie::setExpires().

NOTE: A return value of -1 means that the attribute is not set.

Return Value

int

the currently set expires attribute as seconds since the epoch.

string getExtra(string $name)

Retrieve an extra attribute.

See http\Cookie::setExtra().

Parameters

string $name

The key of the extra attribute.

Return Value

string

the value of the extra attribute.

array getExtras()

Retrieve the list of extra attributes.

See http\Cookie::setExtras().

Return Value

array

the list of extra attributes of the form ["key" => "value"].

int getFlags()

Get the currently set flags.

See http\Cookie::SECURE and http\Cookie::HTTPONLY constants.

Return Value

int

the currently set flags bitmask.

int getMaxAge()

Get the currently set max-age attribute of the cookie list.

See http\Cookie::setMaxAge().

NOTE: A return value of -1 means that the attribute is not set.

Return Value

int

the currently set max-age.

string getPath()

Retrieve the path the cookie(s) of this cookie list are effective at.

See http\Cookie::setPath().

Return Value

string

the effective path.

Cookie setCookie(string $cookie_name, string $cookie_value)

(Re)set a cookie.

See http\Cookie::addCookie() and http\Cookie::setCookies().

NOTE: The cookie will be deleted from the list if $cookie_value is NULL.

Parameters

string $cookie_name

The key of the cookie.

string $cookie_value

The value of the cookie.

Return Value

Cookie self.

Exceptions

InvalidArgumentException

Cookie setCookies(array $cookies = null)

(Re)set the cookies.

See http\Cookie::addCookies().

Parameters

array $cookies

Set the cookies to this array.

Return Value

Cookie self.

Exceptions

InvalidArgumentException

Cookie setDomain(string $value = null)

Set the effective domain of the cookie list.

See http\Cookie::setPath().

Parameters

string $value

The domain the cookie(s) belong to.

Return Value

Cookie self.

Exceptions

InvalidArgumentException

Cookie setExpires(int $value = -1)

Set the traditional expires timestamp.

See http\Cookie::setMaxAge() for a safer alternative.

Parameters

int $value

The expires timestamp as seconds since the epoch.

Return Value

Cookie self.

Exceptions

InvalidArgumentException

Cookie setExtra(string $extra_name, string $extra_value = null)

(Re)set an extra attribute.

See http\Cookie::addExtra().

NOTE: The attribute will be removed from the extras list if $extra_value is NULL.

Parameters

string $extra_name

The key of the extra attribute.

string $extra_value

The value of the extra attribute.

Return Value

Cookie self.

Exceptions

InvalidArgumentException

Cookie setExtras(array $extras = null)

(Re)set the extra attributes.

See http\Cookie::addExtras().

Parameters

array $extras

Set the extra attributes to this array.

Return Value

Cookie self.

Exceptions

InvalidArgumentException

Cookie setFlags(int $value = 0)

Set the flags to specified $value.

See http\Cookie::SECURE and http\Cookie::HTTPONLY constants.

Parameters

int $value

The new flags bitmask.

Return Value

Cookie self.

Exceptions

InvalidArgumentException

Cookie setMaxAge(int $value = -1)

Set the maximum age the cookie may have on the client side.

This is a client clock departure safe alternative to the "expires" attribute. See http\Cookie::setExpires().

Parameters

int $value

The max-age in seconds.

Return Value

Cookie self.

Exceptions

InvalidArgumentException

Cookie setPath(string $path = null)

Set the path the cookie(s) of this cookie list should be effective at.

See http\Cookie::setDomain().

Parameters

string $path

The URL path the cookie(s) should take effect within.

Return Value

Cookie self.

Exceptions

InvalidArgumentException

array toArray()

Get the cookie list as array.

Return Value

array

the cookie list as array.

string toString()

Retrieve the string representation of the cookie list.

See http\Cookie::toArray().

Return Value

string

the cookie list as string.