class Headers implements Iterator (View source)

deprecated Headers will be only accessed via request in the future, if this class stays, then as internal implementation detail.

Container for HTTP header fields

Properties

protected array $fields
protected array $cookies
protected array $cacheDirectives

Methods

__construct(array $fields = [])

Constructs a new Headers object.

static Headers
createFromServer(array $server)

Creates a new Headers instance from the given $_SERVER-superglobal-like array.

void
set(string $name, array|string|DateTime $values, bool $replaceExistingHeader = true)

Sets the specified HTTP header

array
getRaw(string $name)

Get raw header values

array|string
get(string $name)

Returns the specified HTTP header

array
getAll()

Returns all header fields

bool
has(string $name)

Checks if the specified HTTP header exists

void
remove(string $name)

Removes the specified header field

void
setCookie(Cookie $cookie)

Sets a cookie

getCookie(string $name)

Returns a cookie specified by the given name

Cookie[]
getCookies()

Returns all cookies

bool
hasCookie(string $name)

Checks if the specified cookie exists

void
removeCookie(string $name)

Removes the specified cookie if it exists

void
eatCookie(string $name)

Although not 100% semantically correct, an alias for removeCookie()

void
setCacheControlDirective(string $name, string $value = null)

Sets a special directive for use in the Cache-Control header, according to RFC 2616 / 14.9

void
removeCacheControlDirective(string $name)

Removes a special directive previously set for the Cache-Control header.

mixed
getCacheControlDirective(string $name)

Returns the value of the specified Cache-Control directive.

void
setCacheControlDirectivesFromRawHeader(string $rawFieldValue)

Internally sets the cache directives correctly by parsing the given Cache-Control field value.

string
getCacheControlHeader()

Renders and returns a Cache-Control header, based on the previously set cache control directives.

void
setCookiesFromRawHeader(string $rawFieldValue)

Internally sets cookie objects based on the Cookie header field value.

array
getPreparedValues()

Get all header lines prepared as "name: value" strings.

string
__toString()

Renders this headers object as string, with lines separated by "\r\n" as required by RFC 2616 sec 5.

string[]|mixed
current()

No description

void
next()

No description

mixed
key()

No description

bool
valid()

No description

void
rewind()

No description

Details

__construct(array $fields = [])

Constructs a new Headers object.

Parameters

array $fields

Field names and their values (either as single value or array of values)

static Headers createFromServer(array $server)

Creates a new Headers instance from the given $_SERVER-superglobal-like array.

Parameters

array $server

An array similar or equal to $_SERVER, containing headers in the form of "HTTP_FOO_BAR"

Return Value

Headers

void set(string $name, array|string|DateTime $values, bool $replaceExistingHeader = true)

Sets the specified HTTP header

DateTime objects will be converted to a string representation internally but will be returned as DateTime objects on calling get().

Please note that dates are normalized to GMT internally, so that get() will return the same point in time, but not necessarily in the same timezone, if it was not GMT previously. GMT is used synonymously with UTC as per RFC 2616 3.3.1.

Parameters

string $name

Name of the header, for example "Location", "Content-Description" etc.

array|string|DateTime $values

An array of values or a single value for the specified header field

bool $replaceExistingHeader

If a header with the same name should be replaced. Default is true.

Return Value

void

Exceptions

InvalidArgumentException

array getRaw(string $name)

Get raw header values

Parameters

string $name

Return Value

array

array|string get(string $name)

Returns the specified HTTP header

Dates are returned as DateTime objects with the timezone set to GMT.

Parameters

string $name

Name of the header, for example "Location", "Content-Description" etc.

Return Value

array|string

An array of field values if multiple headers of that name exist, a string value if only one value exists and NULL if there is no such header.

array getAll()

Returns all header fields

Note that even for those header fields which exist only one time, the value is returned as an array (with a single item).

Return Value

array

bool has(string $name)

Checks if the specified HTTP header exists

Parameters

string $name

Name of the header

Return Value

bool

void remove(string $name)

Removes the specified header field

Parameters

string $name

Name of the field

Return Value

void

void setCookie(Cookie $cookie)

Sets a cookie

Parameters

Cookie $cookie

Return Value

void

Cookie getCookie(string $name)

Returns a cookie specified by the given name

Parameters

string $name

Name of the cookie

Return Value

Cookie

The cookie or NULL if no such cookie exists

Cookie[] getCookies()

Returns all cookies

Return Value

Cookie[]

bool hasCookie(string $name)

Checks if the specified cookie exists

Parameters

string $name

Name of the cookie

Return Value

bool

void removeCookie(string $name)

Removes the specified cookie if it exists

Note: This will remove the cookie object from this Headers container. If you intend to remove a cookie in the user agent (browser), you should call the cookie's expire() method and not remove the cookie from the Headers container.

Parameters

string $name

Name of the cookie to remove

Return Value

void

void eatCookie(string $name)

Although not 100% semantically correct, an alias for removeCookie()

Parameters

string $name

Name of the cookie to eat

Return Value

void

void setCacheControlDirective(string $name, string $value = null)

Sets a special directive for use in the Cache-Control header, according to RFC 2616 / 14.9

Parameters

string $name

Name of the directive, for example "max-age"

string $value

An optional value

Return Value

void

void removeCacheControlDirective(string $name)

Removes a special directive previously set for the Cache-Control header.

Parameters

string $name

Name of the directive, for example "public"

Return Value

void

mixed getCacheControlDirective(string $name)

Returns the value of the specified Cache-Control directive.

If the cache directive is not present, NULL is returned. If the specified directive is present but contains no value, this method returns true. Finally, if the directive is present and does contain a value, the value is returned.

Parameters

string $name

Name of the cache directive, for example "max-age"

Return Value

mixed

protected void setCacheControlDirectivesFromRawHeader(string $rawFieldValue)

Internally sets the cache directives correctly by parsing the given Cache-Control field value.

Parameters

string $rawFieldValue

The value of a specification compliant Cache-Control header

Return Value

void

See also

set()

protected string getCacheControlHeader()

Renders and returns a Cache-Control header, based on the previously set cache control directives.

Return Value

string

Either the value of the header or NULL if it shall be omitted

See also

get()

protected void setCookiesFromRawHeader(string $rawFieldValue)

Internally sets cookie objects based on the Cookie header field value.

Parameters

string $rawFieldValue

The value of a specification compliant Cookie header

Return Value

void

See also

set()

array getPreparedValues()

Get all header lines prepared as "name: value" strings.

Return Value

array

string __toString()

Renders this headers object as string, with lines separated by "\r\n" as required by RFC 2616 sec 5.

Return Value

string

string[]|mixed current()

No description

Return Value

string[]|mixed

void next()

No description

Return Value

void

mixed key()

No description

Return Value

mixed

bool valid()

No description

Return Value

bool

void rewind()

No description

Return Value

void