Skip to content

Core\Response::setHeader

Sets one header on the response. A header is a name and a value that the client reads before the body, such as Cache-Control: no-store. The server already adds some headers of its own. If you set one of them, your value replaces the server's value.

If you set the same name twice, the header has the last value. Names are compared without upper and lower case, so cache-control and Cache-Control are the same header.

The name contains letters, digits and a few marks such as -. The value contains only printable ASCII characters, so it cannot contain a newline. A newline would let a value start a second header. You cannot set Content-Type: the body method you use sets it. You also cannot set Content-Length, Transfer-Encoding, Connection or Upgrade. The server sets these from the body it sends. For any of these, setHeader throws a LogicError. Text from a visitor is not allowed in the name or the value, and does not compile.

The examples show a page that is not cached, a header that is not allowed, and the headers of an API answer.

Parameters

  • $namestringThe header name, such as Cache-Control. It contains letters, digits and a few marks such as - and _. Spaces and : are not allowed. It cannot be Content-Type, because the body method you use sets that header. It cannot be Content-Length, Transfer-Encoding, Connection or Upgrade. The server sets these from the body it sends. A tainted value is not allowed.
  • $valuestringThe header value. It contains only printable ASCII characters, so it cannot contain a newline. It can be empty. A tainted value is not allowed.

Return value

Returns void. Nothing. If you set the same name twice, the header has the last value. The comparison of names ignores upper and lower case. If the request fails with an error, the response has none of these headers.

Errors

  • LogicError$name is empty, contains a character that is not allowed, or is one of the headers the server sets, such as Content-Type or Content-Length. Or $value contains a character that is not printable ASCII.

Examples

A page that is not cached

<?nvs
// Shows how to tell the browser not to keep a copy of a page.
// `Cache-Control: no-store` tells the browser and every cache to keep no copy.
// Use it for a page with private data, such as an account page.
Core\Response::setHeader('Cache-Control', 'no-store');
Core\Response::sendText("Header set: Cache-Control: no-store\n");
Output
Header set: Cache-Control: no-store

A header that is not allowed

<?nvs
// Shows the two headers that `setHeader` does not allow.
string $report = '';
// A value with a newline would start a second header. `setHeader` throws a `LogicError`.
try {
Core\Response::setHeader('X-Note', "one\nSet-Cookie: admin=1");
} catch (LogicError $error) {
$report = $report . "Not set: a value with a newline.\n";
}
// `Content-Type` is set by the body method, so `setHeader` throws a `LogicError`.
try {
Core\Response::setHeader('content-type', 'text/html');
} catch (LogicError $error) {
$report = $report . "Not set: Content-Type.\n";
}
// This prints both lines. `text` sets `Content-Type: text/plain; charset=utf-8`.
Core\Response::sendText($report);
Output
Not set: a value with a newline.
Not set: Content-Type.

The headers of an api answer

<?nvs
// Shows the headers an API sets on an answer: how long to cache it, and which API version wrote it.
// A client may keep this answer for 60 seconds.
Core\Response::setHeader('Cache-Control', 'max-age=60');
Core\Response::setHeader('X-Api-Version', '2');
// The second call to `Cache-Control` replaces the first, so the value is `max-age=300`.
Core\Response::setHeader('cache-control', 'max-age=300');
Core\Response::sendJson({ cacheControl: 'max-age=300', apiVersion: 2, products: ['Tea', 'Coffee'] });
Output
{"apiVersion":2,"cacheControl":"max-age=300","products":["Tea","Coffee"]}