Core\Session::setLongLived
Core\Session::setLongLived(string $key, mixed $value): voidCore\Session::setLongLived( string $key, mixed $value, ): void
Core\Session::setLongLived() saves a value under a key in the session. A later request from the same visitor reads it with Core\Session::get().
A long-lived value is for a choice the visitor made, such as their language or their cookie consent. It is kept after the browser closes, for [session] long_lived, which is 180 days by default. Do not save a login here. The visitor would stay signed in after closing the browser. For a login, use Core\Session::setShortLived().
A new value replaces the old value of the key, and the key becomes long-lived. The value can be a string, a number, a bool, null, an array or an object.
The method throws a LogicError for a callable. It also throws a LogicError inside Core\Html::later or Core\Task::runAfterResponse, and in a program that handles no request. It throws a RuntimeError when the session store (the place where the server keeps session data) cannot be opened. It throws an IOError when the store cannot be reached.
The examples below show how to remember a language, how to keep a shopping cart, and how a cookie consent stays after a sign-out.
Parameters
$keystringThe key to save the value under. If the key already has a value of either lifetime, it is replaced, and the key gets the lifetime of this call.$valuemixedThe value to save: a string, a number, a bool,null, an array or an object. Ataintedvalue (text that came from the request) is not allowed here, so the call does not compile.
Return value
Returns void. Nothing. When the request ends, the keys it changed are saved to the store. While the session has a long-lived value, the session cookie has a Max-Age, so the browser keeps it after it closes. Do not save a login here: it would stay signed in after the browser closes.
Errors
LogicErrorThe program is not handling a request. A command-line program, a job and a test have no session.LogicErrorThe call is insideCore\Html::laterorCore\Task::runAfterResponse. The response headers are already sent there, so the session cannot change. Most of these calls do not compile.RuntimeErrorThe session store, where the server keeps session data, cannot be opened. This happens only at the first call in the request that uses the session. With the default store, the cause is that the server has no data folder. With[session] backend = "shared",[cache.shared] urlis not set, or the program does not have thecache.sharedcapability.IOErrorThe store cannot be reached. The user may still have a session there, so do not treat them as signed out.LogicError$valuecannot be saved: it is a callable or a resource, or it contains one.RuntimeError[session] cookieis not a valid cookie name. It is thrown when the call must send the cookie.
Changelog
| Version | Change |
|---|---|
0.0.5 | Core\Session::setShortLived saves a value that is deleted when the browser closes, or after [session] short_lived without a request. Core\Session::setLongLived saves a value that stays after the browser closes, for [session] long_lived. Use the short lifetime for a login and the long one for a choice such as a language. |
Examples
The examples below run with this configuration file.
# A session keeps its data in a store. These examples use the Redis server# on this computer at port 16379, and allow the program to use it.[capabilities.cache]shared = true
[session]backend = "shared"
[cache.shared]url = "redis://127.0.0.1:16379"Remember a language
<?nvs// Shows how to remember the language a visitor picked, also after the browser closes.// requires: redis
// On the first visit there is no language yet, so `get()` returns `null`.echo 'before: ', Core\Session::get('language') ?? '(none)', "\n";
// The visitor picks German. `setLongLived()` keeps it for 180 days by default.Core\Session::setLongLived('language', 'de');
// This prints `de`.echo 'after: ', Core\Session::get('language') ?? '(none)', "\n";before: (none)after: deKeep a shopping cart
<?nvs// Shows a shopping cart that is saved in the session, so every page can show it.// requires: redis
// The cart is an array of product codes. On the first page there is no// cart yet, so `get()` returns `null` and `??` gives an empty array.array<tainted string> $cart = (Core\Session::get('cart') ?? []) as array<string>;
// The visitor adds two products. `setLongLived()` saves the whole cart again.// The cart is still there when the visitor opens the browser again tomorrow.$cart[] = 'tea-100g';$cart[] = 'mug-blue';Core\Session::setLongLived('cart', $cart);
// This prints the two product codes.array<tainted string> $saved = (Core\Session::get('cart') ?? []) as array<string>;foreach ($saved as tainted string $code) { echo 'in the cart: ', $code, "\n";}in the cart: tea-100gin the cart: mug-blueSave a cookie consent
<?nvs// Shows how to save a cookie consent choice that stays after the visitor signs out.// requires: redis
// The visitor accepts statistics cookies and declines ads.Core\Session::setLongLived('consent', ['statistics' => true, 'ads' => false]);Core\Session::setShortLived('user', 'ada');
// The visitor signs out. `clearShortLived()` deletes the login and keeps the consent.Core\Session::clearShortLived();
// This prints `statistics: yes` and `ads: no`.array<bool> $consent = (Core\Session::get('consent') ?? []) as array<bool>;foreach ($consent as string $kind => bool $allowed) { echo $kind, ': ', $allowed ? 'yes' : 'no', "\n";}statistics: yesads: no