Core\Response::addCookie
Core\Response::addCookie(string $name, string $value, {…}): voidCore\Response::addCookie( string $name, string $value, {…}, ): void
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 needsSecureandPath=/, and does not allowDomain. A__Secure-prefix needsSecure. 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 istrueif you do not set it.httpOnlybooloptionalWhether the cookie is hidden from scripts in the page. The default is[http.cookies] http_only, which istrueif you do not set it. Set it tofalsehere 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 isLaxif you do not set it.NonewithoutSecurethrows 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.0sdeletes 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$nameis not a valid cookie name, or it breaks the rules of its__Host-or__Secure-prefix. It is also thrown when$value,pathordomaincontains a character that would end the attribute and start another one. It is also thrown whensameSiteisNonewithoutsecure, and whenmaxAgeis 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");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");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 wroteSigning 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");Signed in. Deleted: remember