Core\Session::setShortLived
Core\Session::setShortLived(string $key, mixed $value): voidCore\Session::setShortLived( string $key, mixed $value, ): void
Core\Session::setShortLived() saves a value under a key in the session. A later request from the same visitor reads it with Core\Session::get().
A short-lived value is for a login. It is deleted when the visitor closes the browser. It is also deleted when the visitor sends no request for the time in [session] short_lived, which is 2 hours by default. For a choice the visitor should keep, such as their language, use Core\Session::setLongLived().
A new value replaces the old value of the key, and the key becomes short-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 save a value, how a new value replaces the old one, and how a sign-out deletes a short-lived value.
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. The first short-lived value in a session also sends a second cookie. Its name is [session] cookie with -short added. Two requests that change different keys at the same time keep both changes. If they change the same key, the request that ends last wins.
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"Save a value
<?nvs// Shows how to save the values of a login in the session and read them back.// requires: redis
// `setShortLived()` saves a string, a whole number and a bool under three keys.Core\Session::setShortLived('user', 'ada');Core\Session::setShortLived('user-id', 42);Core\Session::setShortLived('is-admin', false);
// This prints `ada`, `42` and `no`.echo 'user: ', Core\Session::get('user') ?? '(none)', "\n";echo 'user id: ', Core\Session::get('user-id') as ?int ?? 0, "\n";echo 'admin: ', Core\Session::get('is-admin') as bool ? 'yes' : 'no', "\n";user: adauser id: 42admin: noA new value replaces the old one
<?nvs// Shows that saving under a key that already has a value replaces that value.// requires: redis
Core\Session::setShortLived('step', 'address');
// The visitor goes to the next step of the checkout. The second call replaces "address".Core\Session::setShortLived('step', 'payment');
// This prints `payment`.echo 'step: ', Core\Session::get('step') ?? '(none)', "\n";step: paymentSign out deletes it
<?nvs// Shows that a sign-out deletes the short-lived login and keeps the long-lived language.// requires: redis
Core\Session::setShortLived('user', 'ada');Core\Session::setLongLived('language', 'de');
// The visitor signs out. `clearShortLived()` deletes every short-lived value.Core\Session::clearShortLived();
// This prints `(none)` for the user and `de` for the language.echo 'user: ', Core\Session::get('user') ?? '(none)', "\n";echo 'language: ', Core\Session::get('language') ?? '(none)', "\n";user: (none)language: de