Skip to content

Core\Response::addCookie

Adds a cookie to the response. The browser stores it and sends it back with its next requests, where Core\Request::cookie reads it.

A cookie without options is safe by default. It is sent over HTTPS only, JavaScript cannot read it, and other websites cannot send it with their forms. These defaults come from [http.cookies] in nvs.toml, and each call can change them. maxAge sets how long the browser keeps the cookie, and maxAge: 0s deletes it.

A value that contains a space, a comma, a semicolon, a backslash or a quote throws a LogicError. So does a name with the __Host- or __Secure- prefix whose options break the prefix's rules. Each call adds one cookie, and nothing is added when the call throws.

The examples show a cookie with the default options, a value that is not allowed, and the cookies a login page sends.

Parameters

  • $namestringThe cookie name. When the cookie comes back, the name is compared byte for byte, and nothing in it is changed. A __Host- prefix needs Secure and Path=/, and does not allow Domain. A __Secure- prefix needs Secure. A cookie that breaks these rules throws an error.
  • $valuestringThe cookie value. It can contain printable ASCII characters, as RFC 6265 says. A space, a comma, a semicolon, a backslash and a quote are not allowed. Each of them would end the value and start text your program did not write.

Options

  • securebooloptionalWhether the browser sends the cookie only over HTTPS. The default is [http.cookies] secure, which is true if you do not set it.
  • httpOnlybooloptionalWhether the cookie is hidden from scripts in the page. The default is [http.cookies] http_only, which is true if you do not set it. Set it to false here for a cookie that a script must read.
  • sameSiteCore\Response::SameSiteoptionalWhich requests from other sites send the cookie. The default is [http.cookies] same_site, which is Lax if you do not set it. None without Secure throws an error, because every browser ignores that cookie. The same pair in the configuration stops the server from starting.
  • pathstringoptionalThe path prefix under which the browser sends the cookie. The default is [http.cookies] path, which is / if you do not set it.
  • domainstringoptionalThe domain the browser sends the cookie to. By default the cookie has no domain, so the browser sends it only to this host and not to its subdomains. [http.cookies] has no setting for it.
  • maxAgeCore\Time\DurationoptionalHow long the cookie lives. By default it has no age, so it is a session cookie. 0s deletes the cookie. A negative duration throws an error.

Return value

Returns void. Nothing. Each call adds one cookie, so two calls send two Set-Cookie lines. If you add the same name twice, the response sends it twice.

Errors

  • LogicError$name is not a valid cookie name, or it breaks the rules of its __Host- or __Secure- prefix. It is also thrown when $value, path or domain contains a character that would end the attribute and start another one. It is also thrown when sameSite is None without secure, and when maxAge is negative.

Examples

Save a setting

<?nvs
// Shows how to send a cookie with the response.
// This adds the header `Set-Cookie: theme=dark; Path=/; Secure; HttpOnly; SameSite=Lax`.
// The options you leave out come from `[http.cookies]` in `nvs.toml`.
Core\Response::addCookie('theme', 'dark');
// The cookie is in the headers, so the body prints only this line.
Core\Response::sendText("Your theme is saved.\n");
Output
Your theme is saved.

A value with a space

<?nvs
// Shows what happens when a cookie value contains a character that is not allowed.
// A space, a comma, a semicolon, a backslash and a quote are not allowed in a value.
// `addCookie` throws a `LogicError`, and no cookie is added.
tainted string $answer = '';
try {
Core\Response::addCookie('name', 'Ada Lovelace');
$answer = 'saved';
} catch (LogicError $error) {
$answer = $error->message;
}
// This value has no space, so the cookie is added.
Core\Response::addCookie('name', 'Ada_Lovelace');
Core\Response::sendText($answer . "\n");
Output
Core\Response::addCookie(): `Ada Lovelace` is not a value a cookie can carry — a space, a comma, a semicolon, a backslash, a quote and anything outside printable ASCII would end the value and begin an attribute the program never wrote

Signing in

<?nvs
// Shows the cookies a login page sends when a user signs in.
string $token = 'k3v9Qx7wLm2p';
// The `__Host-` prefix tells the browser to send this cookie to this host only.
Core\Response::addCookie('__Host-session', $token, {
// Other websites cannot send this cookie, not even with their links.
sameSite: Core\Response::SameSite::Strict,
// The browser deletes the cookie after eight hours.
maxAge: 8h,
});
// `maxAge: 0s` deletes a cookie. This request sends an old `remember` cookie,
// so the program deletes it and this prints "Signed in. Deleted: remember".
string $said = 'Signed in.';
if (Core\Request::cookie('remember') != null) {
Core\Response::addCookie('remember', '', { maxAge: 0s });
$said = $said . ' Deleted: remember';
}
Core\Response::sendText($said . "\n");
Output
Signed in. Deleted: remember