Core\Request::headers
Core\Request::headers(): array<array<tainted string>>Returns every header of the request in one array. Each key is the name of a header, written in lower case, so Content-Type arrives as content-type. The keys are in the order the headers first arrived.
The value of each key is a list of strings. A request can have the same header on more than one line, and the list has one entry for each line, in the order they arrived. So no value is lost. Core\Request::header returns one header with its lines joined into one string.
When the request has no headers, the array is empty. Every string in it is tainted, which means it came from outside your program. A command-line program answers no request, so headers throws a LogicError there.
Return value
Returns array<array<tainted string>>. An array<array<tainted string>>. Each key is a header name in lower case. Its value is a list with one entry for each line of that header, in the order the lines arrived. The array is empty when the request has no headers.
Errors
LogicErrorThe program is not handling a request.ParseErrorThe value of a header is not valid UTF-8.
Examples
Listing every header
<?nvs// Shows how to list every header of a request with its value.
// `Core\Request::headers` returns each header name in lower case, with a list of its values.// This prints "host: example.com", then "user-agent: curl/8.5.0", then "accept: */*".foreach (Core\Request::headers() as tainted string $name => array<tainted string> $values) { foreach ($values as tainted string $value) { echo $name, ': ', $value, "\n"; }}host: example.comuser-agent: curl/8.5.0accept: */*A header on two lines
<?nvs// Shows that a header sent on two lines keeps both values as two entries.
array<array<tainted string>> $headers = Core\Request::headers();
// The request has two `Accept` lines. The key is "accept", and its list has two entries.if (Core\Arr::hasKey($headers, 'accept')) { // This prints "2". echo Core\Arr::count($headers['accept']), "\n";
// This prints "text/html", then "application/json". foreach ($headers['accept'] as tainted string $type) { echo $type, "\n"; }}2text/htmlapplication/jsonLogging a request without its secrets
<?nvs// Shows how to write the headers of a request to a log, with the secret values hidden.
// These headers can contain a password or a session, so the log shows "[hidden]" for them.// The list is `tainted` because `Core\Arr::contains` compares it with header names from the request.array<tainted string> $secret = ['authorization', 'cookie', 'x-api-key'];
// The names are already in lower case, so one comparison is enough.// This prints five lines. The lines for `authorization` and `cookie` show "[hidden]".foreach (Core\Request::headers() as tainted string $name => array<tainted string> $values) { foreach ($values as tainted string $value) { if (Core\Arr::contains($secret, $name)) { echo $name, ": [hidden]\n"; } else { echo $name, ': ', $value, "\n"; } }}host: example.comauthorization: [hidden]content-type: application/jsoncookie: [hidden]accept: application/json