Skip to content

Core\Test\Response::header

$response->header(string $name): ?string$response->header(
    string $name,
): ?string

header() returns the value of one header of the answer to a request that Core\Test::request() sent. Upper and lower case in the name do not matter. If the answer has no header of that name, the result is null.

These are the headers your program sends to a visitor. Content-Type is always there. If the program did not set it, its value is text/html; charset=utf-8. A redirect sets Location, and Core\Response::setHeader sets other headers. The server also adds headers of its own, such as Content-Length and Date. header() does not return those.

When a header has more than one line, header() joins the values with , . Each cookie has its own Set-Cookie line, and two joined cookies are not a cookie. So header("Set-Cookie") throws a LogicError. Use cookies() or headers() to read cookies.

A request that failed has no headers, so the result is always null.

The examples below show how to read the content type, how to test a redirect, and what header() does with Set-Cookie.

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 ?string. The value, exactly as the program set it. The result is null when the answer has no header of that name. When the answer has the header on more than one line, the values are joined with , in order. The answer always has a Content-Type header. If the program set none, its value is text/html; charset=utf-8, the same as the server sends. A request that failed has no headers.

Errors

  • LogicErrorThe name is Set-Cookie. Two cookies joined into one string are not a cookie, so cookies() and headers() read that header.

Examples

Read the content type

<?nvs
// Shows how to read the `Content-Type` header of an answer.
use Core\Http\Method;
use Core\Test;
if (Core\Env::context() == Core\Env::Context::Request) {
// This part answers each request. `Core\Response::sendJson` sends the array
// as JSON and sets the content type.
Core\Response::sendJson(['ok' => true]);
} else {
// This part is the test.
var $response = Test::request(Method::Get, '/health');
// Upper and lower case do not matter in a header name. This prints `application/json`.
echo $response->header('content-type'), "\n";
}
Output
application/json

Test a redirect

<?nvs
// Shows how to test that a page sends the visitor to another address.
use Core\Http\Method;
use Core\Test;
if (Core\Env::context() == Core\Env::Context::Request) {
// This part answers each request. The old address redirects to the new one.
var $path = Core\Request::path();
if ($path == '/old-page') {
Core\Response::redirect('/new-page');
}
} else {
// This part is the test.
var $response = Test::request(Method::Get, '/old-page');
// A redirect sets the status and the `Location` header. This prints `303 /new-page`.
echo $response->status(), ' ', $response->header('Location'), "\n";
// The answer has no `Retry-After` header, so the result is `null`.
// This prints `none`.
echo $response->header('Retry-After') ?? 'none', "\n";
}
Output
303 /new-page
none

Set cookie needs another method

<?nvs
// Shows what `header()` does when you ask for `Set-Cookie`.
use Core\Http\Method;
use Core\Test;
if (Core\Env::context() == Core\Env::Context::Request) {
// This part answers each request. It sets two cookies.
Core\Response::addCookie('theme', 'dark');
Core\Response::addCookie('lang', 'en');
} else {
var $response = Test::request(Method::Get, '/settings');
// Each cookie has its own `Set-Cookie` line. `header()` cannot join them,
// so it throws a `LogicError`. This prints `use cookies()`.
try {
$response->header('Set-Cookie');
} catch (LogicError $error) {
echo "use cookies()\n";
}
// `cookies()` returns each cookie by its name. This prints `dark`.
echo $response->cookies()['theme'], "\n";
}
Output
use cookies()
dark