Skip to content

Core\Request::header

Returns the value of one header of the request. You give the name of the header. The result is its value as a string, or null when the request has no header of that name.

Upper and lower case in the name do not matter, so Content-Type and content-type return the same value. A header that arrived empty returns an empty string.

A request can have the same header on more than one line. Then the values are joined into one string, with , between them, in the order they arrived. Core\Request::headers returns each line on its own.

The result is tainted, which means it came from outside your program. A command-line program answers no request, so header throws a LogicError there.

Parameters

  • $namestringThe name of the header. Upper and lower case do not matter, so Content-Type and content-type are the same header.

Return value

Returns ?tainted string. The value as a tainted string, exactly as it arrived. The result is null when the request has no header of that name. When the header arrived on more than one line, the values are joined with , in the order they arrived. headers() returns each line on its own.

Errors

  • LogicErrorThe program is not handling a request.
  • ParseErrorThe value of this header is not valid UTF-8.

Examples

Reading a header

<?nvs
// Shows how to read one header of a request, such as the browser name in `User-Agent`.
// `Core\Request::header` returns the value of the header with this name.
// Upper and lower case do not matter, so both lines print "Mozilla/5.0 (X11; Linux x86_64)".
echo Core\Request::header('User-Agent') ?? 'none', "\n";
echo Core\Request::header('user-agent') ?? 'none', "\n";
Output
Mozilla/5.0 (X11; Linux x86_64)
Mozilla/5.0 (X11; Linux x86_64)

A header sent twice or not at all

<?nvs
// Shows what `Core\Request::header` returns for a header sent twice, and for a header not sent.
// The request has two `Accept` lines. The values are joined with ", ".
// This prints "text/html, application/json".
echo Core\Request::header('Accept') ?? 'none', "\n";
// The request has no `Referer` header, so the result is `null`.
// `??` gives "none" instead. This prints "none".
echo Core\Request::header('Referer') ?? 'none', "\n";
Output
text/html, application/json
none

Checking an api token

<?nvs
// Shows how an API reads the access token that a client sends in the `Authorization` header.
// The server knows each token and the client it belongs to.
array<string> $clients = ['s7f3a9c2e1' => 'shop', 'b4d8e0f6a2' => 'blog'];
// The header looks like "Bearer s7f3a9c2e1". `Core\Str::after` returns the text after "Bearer ".
tainted string $header = Core\Request::header('Authorization') ?? '';
?tainted string $token = null;
if (Core\Str::startsWith($header, 'Bearer ')) {
$token = Core\Str::after($header, 'Bearer ');
}
// `??` gives "" when the token is missing or unknown. This prints "hello, shop".
string $client = $clients[$token ?? ''] ?? '';
if ($client != '') {
echo 'hello, ', $client, "\n";
} else {
echo "401: send a valid token\n";
}
Output
hello, shop