Skip to main content
Version: Fegg 5.x

Application::setCookie

Set COOKIE data

Description​

public void Application::setCookie( string $name, string $value, int $expire = '', string $path = '/', string $sameSite = 'Strict' )

Set COOKIE data. The cookie is always HttpOnly (JavaScript cannot read it). It is Secure (sent back over HTTPS only) when the request is HTTPS, or when $sameSite is None, which browsers require. Over plain HTTP, on a local development server for example, it works without Secure.

Arguments​

NameData typeDefaultRemarks
$namestringCookie name
$valuestringValue
$expireint''Lifetime in seconds from now. When omitted (or 0), 1 week (604800 seconds)
$pathstring'/'Path where the cookie is valid
$sameSitestring'Strict'SameSite attribute: Strict, Lax or None

Example​

Controller
$this->setCookie('foo', 'bar');

Set the lifetime in seconds with the third argument.

Controller
// This cookie expires after 60 seconds.
$this->setCookie('foo', 'bar', 60);

Set the path with the fourth argument and the SameSite attribute with the fifth.

Controller
// This cookie is only valid under "/test/".
$this->setCookie('foo', 'bar', 0, '/test/');

// Sent back when the visitor follows a link from another site (SameSite=Lax)
$this->setCookie('campaign', 'spring', 86400 * 30, '/', 'Lax');