Core\Request::cookie
Core\Request::cookie(string $name): ?tainted stringCore\Request::cookie( string $name, ): ?tainted string
Returns the value of one cookie that the browser sent with the request. You give the name of the cookie. The result is its value as a string, or null when the request has no cookie of that name.
The name must match exactly. Upper and lower case are different, and a.b and a_b are two different cookies. The value is returned exactly as it arrived, and nothing is decoded.
When two cookies have the same name, the first one is returned. A cookie whose name starts with __Host- can exist only once in a browser. So when two of them arrive, the result is null.
The result is tainted, which means it came from outside your program. A command-line program answers no request, so cookie throws a LogicError there.
Parameters
$namestringThe name of the cookie, exactly as it was set. Upper and lower case are different, anda.banda_bare two different cookies.
Return value
Returns ?tainted string. The value as a tainted string, exactly as it arrived. Nothing is decoded. The result is null when the request has no cookie of that name. When the name starts with __Host- and the cookie arrived twice, the result is null too.
Errors
LogicErrorThe program is not handling a request.ParseErrorThe value of this cookie is not valid UTF-8.
Examples
Reading a cookie
<?nvs// Shows how to read the colour theme that a visitor chose, from a cookie.
// `Core\Request::cookie` returns the value of the cookie named "theme".// `??` gives "light" when the request has no such cookie.tainted string $theme = Core\Request::cookie('theme') ?? 'light';
// This prints "theme: dark".echo 'theme: ', $theme, "\n";theme: darkA cookie that was not sent
<?nvs// Shows what `Core\Request::cookie` returns for a cookie the request does not have.
// The request has no cookie named "cart", so the result is `null`.?tainted string $cart = Core\Request::cookie('cart');echo $cart == null ? "no cart\n" : "cart found\n";
// The request has a cookie named "lang", but not one named "Lang".// Upper and lower case are different, so this result is `null` too.?tainted string $language = Core\Request::cookie('Lang');echo $language == null ? "no Lang\n" : "Lang found\n";
// The exact name returns the value. This prints "lang: de".echo 'lang: ', Core\Request::cookie('lang') ?? 'none', "\n";no cartno Langlang: deFinding the logged in user
<?nvs// Shows how a website finds the logged-in user from a session cookie.
// The server keeps a list of open sessions. Each has a token and a user name.array<string> $sessions = ['k9f2c1d7' => 'ada', 'p3m8x0q4' => 'linus'];
// The browser sends the token in the cookie `__Host-session`.// The prefix `__Host-` tells the browser to send it only to this exact host.tainted string $token = Core\Request::cookie('__Host-session') ?? '';
// `??` gives "" when no session has this token. This prints "logged in as ada".string $user = $sessions[$token] ?? '';if ($user != '') { echo 'logged in as ', $user, "\n";} else { echo "please log in\n";}logged in as ada