{cookie:name}

Description

Reads HTTP cookies sent by the visitors browser. With a cookie name, it returns that cookies value, or an empty string when the visitor has no such cookie. With no name, it returns every cookie as a JSON object (the raw PHP $_COOKIE array, json-encoded). The output reflects the current request, so it is not cacheable and not a fixed value - it changes per visitor. The name is trimmed before lookup. Cookie values come straight from the browser and are not trusted input; escape them before use and never rely on them for authorization.

Parameters

name optional

Name of the cookie to read. When set, returns that cookies value (or empty string if the visitor has no such cookie). When left empty, returns all cookies encoded as a JSON object.

Examples

test[{cookie:nonexistent_demo}]
Expected[]
Actual[]
A cookie that the visitor does not have returns an empty string. The brackets here just make the empty result visible. This is the one deterministic part of cookie: any unset name yields nothing.
virtual{cookie:auth_email}
Expected(the value stored in the auth_email cookie, e.g. jane@example.org)
With a name, cookie returns that cookies value as sent by the visitors browser. The output depends on the request, so this is illustrative, not a fixed test.
virtual{cookie:}
Expected(every cookie as a JSON object, e.g. {"lang":"en","theme":"dark"})
Actual{"AA_Session":"ee5cf6a83a0b6800c7820743ead605a8"}
With no name, cookie returns the whole $_COOKIE array json-encoded. Useful for debugging what the browser is sending. Runtime-dependent, so illustrative.
virtual{ifset:{cookie:lang}:{cookie:lang}:en}
Expected(the lang cookie value, or en when the cookie is not set)
Actualen
The real-world pattern: read a preference cookie and supply a default when it is missing. ifset treats the empty string from an unset cookie as not set.