Core\Session::clearShortLived
Core\Session::clearShortLived(): voidCore\Session::clearShortLived() deletes every short-lived value from the session of the current request. The long-lived values are kept.
Call it when a user signs out. A short-lived value is the kind a sign-in uses. The browser deletes it when it closes. A long-lived value, such as a cookie choice or a cart, is kept after the browser closes. To delete only those, use Core\Session::clearLongLived().
The session also gets a new identifier, and the response sends it in the session cookie. A copy of the old cookie no longer finds the session. The session stays open, so you can save new values at once.
It throws a LogicError outside a request, or inside Core\Html::later or Core\Task::runAfterResponse. 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 old identifier may then still work, so do not treat the user as signed out.
The examples below show a sign-out that keeps a preference, a session that is used again at once, and a sign-out page.
Return value
Returns void. Nothing. The session gets a new identifier, and the response sends it in the session cookie. The old identifier no longer finds the session. Short-lived values that another request saved in the meantime are deleted too.
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 store cannot be opened, or[session] cookieis not a valid cookie name.IOErrorThe store cannot be reached. The old identifier may still work, so do not treat the user as signed out.
Changelog
| Version | Change |
|---|---|
0.0.5 | Core\Session::clearShortLived deletes every short-lived value, and Core\Session::clearLongLived deletes every long-lived value. Both give the session a new identifier. Core\Session::clearAll deletes the whole session and tells the browser to delete both session cookies. |
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"Sign out
<?nvs// Shows how to sign a user out and keep the user's preferences.// requires: redis
// The user signed in earlier. The user also chose a dark theme.Core\Session::setShortLived('user', 'ada');Core\Session::setLongLived('theme', 'dark');
// The user signs out. `clearShortLived()` deletes `user`, and `theme` is kept.// This prints `user: (nobody)` and `theme: dark`.Core\Session::clearShortLived();echo 'user: ', Core\Session::get('user') ?? '(nobody)', "\n";echo 'theme: ', Core\Session::get('theme') ?? '(default)', "\n";user: (nobody)theme: darkThe session is used again
<?nvs// Shows that you can save new values in the session right after `clearShortLived()`.// requires: redis
Core\Session::setShortLived('visits', 3);Core\Session::clearShortLived();
// The session is still open. This saves a new value and prints `visits: 1`.Core\Session::setShortLived('visits', 1);echo 'visits: ', Core\Session::get('visits') as int, "\n";
// You can call `clearShortLived()` again. A second call is allowed too.// This prints `visits: (none)`.Core\Session::clearShortLived();Core\Session::clearShortLived();echo 'visits: ', Core\Session::get('visits') ?? '(none)', "\n";visits: 1visits: (none)A sign out page
<?nvs// Shows a sign-out page that keeps the visitor's cookie choice.// requires: redis
class Account{ public static function signOut(): void { if (Core\Session::get('user') == null) { echo "you are not signed in\n"; return; } echo 'goodbye, ', Core\Session::get('user'), "\n"; Core\Session::clearShortLived(); }}
// An earlier request saved these two values.Core\Session::setShortLived('user', 'ada');Core\Session::setLongLived('cookie-consent', 'analytics');
// The first call prints `goodbye, ada`. The second prints `you are not signed in`.Account::signOut();Account::signOut();
// The cookie choice is kept. This prints `consent: analytics`.echo 'consent: ', Core\Session::get('cookie-consent') ?? '(not asked yet)', "\n";goodbye, adayou are not signed inconsent: analytics