Core\Response::setHeader
Core\Response::setHeader(string $name, string $value): voidCore\Response::setHeader( string $name, string $value, ): void
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 asCache-Control. It contains letters, digits and a few marks such as-and_. Spaces and:are not allowed. It cannot beContent-Type, because the body method you use sets that header. It cannot beContent-Length,Transfer-Encoding,ConnectionorUpgrade. The server sets these from the body it sends. Ataintedvalue is not allowed.$valuestringThe header value. It contains only printable ASCII characters, so it cannot contain a newline. It can be empty. Ataintedvalue 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$nameis empty, contains a character that is not allowed, or is one of the headers the server sets, such asContent-TypeorContent-Length. Or$valuecontains 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");Header set: Cache-Control: no-storeA 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);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'] });{"apiVersion":2,"cacheControl":"max-age=300","products":["Tea","Coffee"]}