Cookies
extends Headers
in package
A Laminas\Http\Cookies object is designed to contain and maintain HTTP cookies, and should be used along with Laminas\Http\Client in order to manage cookies across HTTP requests and responses.
The class contains an array of Laminas\Http\Header\Cookie objects. Cookies can be added automatically from a request or manually. Then, the Cookies class can find and return the cookies needed for a specific HTTP request.
A special parameter can be passed to all methods of this class that return cookies: Cookies can be returned either in their native form (as Laminas\Http\Header\Cookie objects) or as strings - the later is suitable for sending as the value of the "Cookie" header in an HTTP request. You can also choose, when returning more than one cookie, whether to get an array of strings (by passing Laminas\Http\Client\Cookies::COOKIE_STRING_ARRAY) or one unified string for all cookies (by passing Laminas\Http\Client\Cookies::COOKIE_STRING_CONCAT).
Tags
Table of Contents
Constants
- COOKIE_OBJECT = 0
- Return cookie(s) as a Laminas\Http\Cookie object
- COOKIE_STRING_ARRAY = 1
- Return cookie(s) as a string (suitable for sending in an HTTP request)
- COOKIE_STRING_CONCAT = 2
- Return all cookies as one long string (suitable for sending in an HTTP request)
- COOKIE_STRING_CONCAT_STRICT = 3
- Return all cookies as one long string (strict mode) - Single space after the semi-colon separating each cookie - Remove trailing semi-colon, if any
Properties
- $cookies : array<string|int, mixed>
- $headers : Headers
- $headersKeys : array<string|int, mixed>
- $pluginClassLoader : PluginClassLocator
- $rawCookies : array<string|int, mixed>
Methods
- addCookie() : mixed
- Add a cookie to the class. Cookie should be passed either as a Laminas\Http\Header\SetCookie object or as a string - in which case an object is created from the string.
- addCookiesFromResponse() : mixed
- Parse an HTTP response, adding all the cookies set in that response
- addHeader() : $this
- Add a Header to this container, for raw values @see addHeaderLine() and addHeaders()
- addHeaderLine() : $this
- Add a raw header line, either in name => value, or as a single string 'name: value'
- addHeaders() : $this
- Add many headers at once
- clearHeaders() : $this
- Clear all headers
- count() : int
- Return the number of headers in this contain, if all headers have not been parsed, actual count could increase if MultipleHeader objects exist in the Request/Response. If you need an exact count, iterate
- current() : array<string|int, mixed>|HeaderInterface
- Return the current value for this iterator, lazy loading it if need be
- forceLoading() : bool
- By calling this, it will force parsing and loading of all headers, after this count() will be accurate
- fromResponse() : static
- Create a new Cookies object and automatically load into it all the cookies set in a Response object. If $uri is set, it will be considered as the requested URI for setting default domain and path of the cookie.
- fromString() : void
- Populates headers from string representation
- get() : bool|HeaderInterface|ArrayIterator
- Get all headers of a certain name/type
- getAllCookies() : array<string|int, mixed>|string
- Get all cookies in the cookie jar as an array
- getCookie() : SetCookie|string
- Get a specific cookie according to a URI and name
- getMatchingCookies() : array<string|int, mixed>|string
- Return an array of all cookies matching a specific request according to the request URI, whether session cookies should be sent or not, and the time to consider as "now" when checking cookie expiry time.
- getPluginClassLoader() : PluginClassLocator
- Return an instance of a PluginClassLocator, lazyload and inject map if necessary
- has() : bool
- Test for existence of a type of header
- isEmpty() : bool
- Tells if the array of cookies is empty
- key() : mixed
- Return the current key for this object as an iterator
- next() : void
- Advance the pointer for this object as an iterator
- removeHeader() : bool
- Remove a Header from the container
- reset() : $this
- Empties the cookieJar of any cookie
- rewind() : void
- Reset the internal pointer for this object as an iterator
- setPluginClassLoader() : $this
- Set an alternate implementation for the PluginClassLoader
- toArray() : array<string|int, mixed>
- Return the headers container as an array
- toString() : string
- Render all headers at once
- valid() : bool
- Is this iterator still valid?
- _flattenCookiesArray() : array<string|int, mixed>|string
- Helper function to recursively flatten an array. Should be used when exporting the cookies array (or parts of it)
- _matchDomain() : array<string|int, mixed>
- Return a subset of the cookies array matching a specific domain
- _matchPath() : array<string|int, mixed>
- Return a subset of a domain-matching cookies that also match a specified path
- createKey() : string
- Create array key from header name
- lazyLoadHeader() : mixed|void
Constants
COOKIE_OBJECT
Return cookie(s) as a Laminas\Http\Cookie object
public
mixed
COOKIE_OBJECT
= 0
COOKIE_STRING_ARRAY
Return cookie(s) as a string (suitable for sending in an HTTP request)
public
mixed
COOKIE_STRING_ARRAY
= 1
COOKIE_STRING_CONCAT
Return all cookies as one long string (suitable for sending in an HTTP request)
public
mixed
COOKIE_STRING_CONCAT
= 2
COOKIE_STRING_CONCAT_STRICT
Return all cookies as one long string (strict mode) - Single space after the semi-colon separating each cookie - Remove trailing semi-colon, if any
public
mixed
COOKIE_STRING_CONCAT_STRICT
= 3
Properties
$cookies
protected
array<string|int, mixed>
$cookies
= []
$headers
protected
Headers
$headers
$headersKeys
protected
array<string|int, mixed>
$headersKeys
= []
key names for $headers array
$pluginClassLoader
protected
PluginClassLocator
$pluginClassLoader
$rawCookies
protected
array<string|int, mixed>
$rawCookies
Methods
addCookie()
Add a cookie to the class. Cookie should be passed either as a Laminas\Http\Header\SetCookie object or as a string - in which case an object is created from the string.
public
addCookie(SetCookie|string $cookie[, Uri|string $refUri = null ]) : mixed
Parameters
- $cookie : SetCookie|string
- $refUri : Uri|string = null
-
Optional reference URI (for domain, path, secure)
Tags
addCookiesFromResponse()
Parse an HTTP response, adding all the cookies set in that response
public
addCookiesFromResponse(Response $response, Uri|string $refUri) : mixed
Parameters
addHeader()
Add a Header to this container, for raw values @see addHeaderLine() and addHeaders()
public
addHeader(HeaderInterface $header) : $this
Parameters
- $header : HeaderInterface
Return values
$thisaddHeaderLine()
Add a raw header line, either in name => value, or as a single string 'name: value'
public
addHeaderLine(string $headerFieldNameOrLine[, string $fieldValue = null ]) : $this
This method allows for lazy-loading in that the parsing and instantiation of Header object will be delayed until they are retrieved by either get() or current()
Parameters
- $headerFieldNameOrLine : string
- $fieldValue : string = null
-
optional
Tags
Return values
$thisaddHeaders()
Add many headers at once
public
addHeaders(array<string|int, mixed>|Traversable $headers) : $this
Expects an array (or Traversable object) of type/value pairs.
Parameters
- $headers : array<string|int, mixed>|Traversable
Tags
Return values
$thisclearHeaders()
Clear all headers
public
clearHeaders() : $this
Removes all headers from queue
Return values
$thiscount()
Return the number of headers in this contain, if all headers have not been parsed, actual count could increase if MultipleHeader objects exist in the Request/Response. If you need an exact count, iterate
public
count() : int
Attributes
Return values
int —count of currently known headers
current()
Return the current value for this iterator, lazy loading it if need be
public
current() : array<string|int, mixed>|HeaderInterface
Attributes
Return values
array<string|int, mixed>|HeaderInterfaceforceLoading()
By calling this, it will force parsing and loading of all headers, after this count() will be accurate
public
forceLoading() : bool
Return values
boolfromResponse()
Create a new Cookies object and automatically load into it all the cookies set in a Response object. If $uri is set, it will be considered as the requested URI for setting default domain and path of the cookie.
public
static fromResponse(Response $response, Uri|string $refUri) : static
Parameters
Tags
Return values
staticfromString()
Populates headers from string representation
public
static fromString(string $string) : void
Parameters
- $string : string
Tags
get()
Get all headers of a certain name/type
public
get(string $name) : bool|HeaderInterface|ArrayIterator
Parameters
- $name : string
Return values
bool|HeaderInterface|ArrayIteratorgetAllCookies()
Get all cookies in the cookie jar as an array
public
getAllCookies([int $retAs = self::COOKIE_OBJECT ]) : array<string|int, mixed>|string
Parameters
- $retAs : int = self::COOKIE_OBJECT
-
Whether to return cookies as objects of \Laminas\Http\Header\SetCookie or as strings
Return values
array<string|int, mixed>|stringgetCookie()
Get a specific cookie according to a URI and name
public
getCookie(Uri|string $uri, string $cookieName[, int $retAs = self::COOKIE_OBJECT ]) : SetCookie|string
Parameters
- $uri : Uri|string
-
The uri (domain and path) to match
- $cookieName : string
-
The cookie's name
- $retAs : int = self::COOKIE_OBJECT
-
Whether to return cookies as objects of \Laminas\Http\Header\SetCookie or as strings
Tags
Return values
SetCookie|stringgetMatchingCookies()
Return an array of all cookies matching a specific request according to the request URI, whether session cookies should be sent or not, and the time to consider as "now" when checking cookie expiry time.
public
getMatchingCookies(string|Uri $uri[, bool $matchSessionCookies = true ][, int $retAs = self::COOKIE_OBJECT ][, int $now = null ]) : array<string|int, mixed>|string
Parameters
- $uri : string|Uri
-
URI to check against (secure, domain, path)
- $matchSessionCookies : bool = true
-
Whether to send session cookies
- $retAs : int = self::COOKIE_OBJECT
-
Whether to return cookies as objects of \Laminas\Http\Header\Cookie or as strings
- $now : int = null
-
Override the current time when checking for expiry time
Tags
Return values
array<string|int, mixed>|stringgetPluginClassLoader()
Return an instance of a PluginClassLocator, lazyload and inject map if necessary
public
getPluginClassLoader() : PluginClassLocator
Return values
PluginClassLocatorhas()
Test for existence of a type of header
public
has(string $name) : bool
Parameters
- $name : string
Return values
boolisEmpty()
Tells if the array of cookies is empty
public
isEmpty() : bool
Return values
boolkey()
Return the current key for this object as an iterator
public
key() : mixed
Attributes
next()
Advance the pointer for this object as an iterator
public
next() : void
Attributes
removeHeader()
Remove a Header from the container
public
removeHeader(HeaderInterface $header) : bool
Parameters
- $header : HeaderInterface
Return values
boolreset()
Empties the cookieJar of any cookie
public
reset() : $this
Return values
$thisrewind()
Reset the internal pointer for this object as an iterator
public
rewind() : void
Attributes
setPluginClassLoader()
Set an alternate implementation for the PluginClassLoader
public
setPluginClassLoader(PluginClassLocator $pluginClassLoader) : $this
Parameters
- $pluginClassLoader : PluginClassLocator
Return values
$thistoArray()
Return the headers container as an array
public
toArray() : array<string|int, mixed>
Tags
Return values
array<string|int, mixed>toString()
Render all headers at once
public
toString() : string
This method handles the normal iteration of headers; it is up to the concrete classes to prepend with the appropriate status/request line.
Return values
stringvalid()
Is this iterator still valid?
public
valid() : bool
Attributes
Return values
bool_flattenCookiesArray()
Helper function to recursively flatten an array. Should be used when exporting the cookies array (or parts of it)
protected
_flattenCookiesArray(SetCookie|array<string|int, mixed> $ptr[, int $retAs = self::COOKIE_OBJECT ]) : array<string|int, mixed>|string
Parameters
- $ptr : SetCookie|array<string|int, mixed>
- $retAs : int = self::COOKIE_OBJECT
-
What value to return
Return values
array<string|int, mixed>|string_matchDomain()
Return a subset of the cookies array matching a specific domain
protected
_matchDomain(string $domain) : array<string|int, mixed>
Parameters
- $domain : string
Return values
array<string|int, mixed>_matchPath()
Return a subset of a domain-matching cookies that also match a specified path
protected
_matchPath(array<string|int, mixed> $domains, string $path) : array<string|int, mixed>
Parameters
- $domains : array<string|int, mixed>
- $path : string
Return values
array<string|int, mixed>createKey()
Create array key from header name
protected
static createKey(string $name) : string
Parameters
- $name : string
Return values
stringlazyLoadHeader()
protected
lazyLoadHeader(int|string $index[, bool $isGeneric = false ]) : mixed|void
Parameters
- $index : int|string
- $isGeneric : bool = false
-
If true, there is no need to parse $index and call the ClassLoader.