Skip to content

Core\Session::clearLongLived

Core\Session::clearLongLived(): void

Core\Session::clearLongLived() deletes every long-lived value from the session of the current request. The short-lived values are kept.

A long-lived value is the kind a preference uses, such as a cookie choice or a cart. It is kept after the browser closes. A short-lived value is the kind a sign-in uses. The browser deletes it when it closes. Call clearLongLived() when a visitor withdraws a consent, and the user stays signed in. To sign a user out, use Core\Session::clearShortLived().

A key has the lifetime of its last write. The session also gets a new identifier. The session cookie no longer has a Max-Age (the time the browser keeps a cookie), so the browser deletes it when it closes. 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 examples below delete preferences and keep the user signed in. They also show the lifetime of the last write, and a button that withdraws a cookie consent.

Return value

Returns void. Nothing. The session gets a new identifier, and the response sends it in the session cookie. The cookie has no Max-Age any more, so the browser deletes it when it closes. Long-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 inside Core\Html::later or Core\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] cookie is 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

VersionChange
0.0.5Core\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.

nvs.toml
# 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"

Forget the preferences

<?nvs
// Shows how to delete a visitor's saved preferences and keep the user signed in.
// requires: redis
Core\Session::setShortLived('user', 'ada');
Core\Session::setLongLived('theme', 'dark');
Core\Session::setLongLived('language', 'de');
// `clearLongLived()` deletes `theme` and `language`. The user stays signed in.
Core\Session::clearLongLived();
echo 'user: ', Core\Session::get('user') ?? '(nobody)', "\n";
echo 'theme: ', Core\Session::get('theme') ?? '(default)', "\n";
echo 'language: ', Core\Session::get('language') ?? '(default)', "\n";
Output
user: ada
theme: (default)
language: (default)

The last write sets the lifetime

<?nvs
// Shows that `clearLongLived()` deletes a key only when its last write was long-lived.
// requires: redis
// `language` is saved as long-lived, then saved again as short-lived.
// The second write makes it short-lived.
Core\Session::setLongLived('language', 'de');
Core\Session::setShortLived('language', 'fr');
Core\Session::setLongLived('theme', 'dark');
// This prints `language: fr` and `theme: (default)`.
Core\Session::clearLongLived();
echo 'language: ', Core\Session::get('language') ?? '(default)', "\n";
echo 'theme: ', Core\Session::get('theme') ?? '(default)', "\n";
Output
language: fr
theme: (default)

Withdraw cookie consent

<?nvs
// Shows a "Withdraw consent" button that deletes the cookie choice and the cart.
// requires: redis
class Privacy
{
public static function withdrawConsent(): void
{
Core\Session::clearLongLived();
echo "your choices were deleted\n";
}
}
// Earlier requests saved these values.
Core\Session::setShortLived('user', 'ada');
Core\Session::setLongLived('cookie-consent', 'analytics');
Core\Session::setLongLived('cart', [3, 17]);
// The request below is for `/privacy/withdraw`.
if (Core\Request::path() == '/privacy/withdraw') {
Privacy::withdrawConsent();
}
// This prints `consent: (not asked yet)`, `cart: (empty)` and `user: ada`.
echo 'consent: ', Core\Session::get('cookie-consent') ?? '(not asked yet)', "\n";
echo 'cart: ', Core\Session::get('cart') == null ? '(empty)' : 'has items', "\n";
echo 'user: ', Core\Session::get('user') ?? '(nobody)', "\n";
Output
your choices were deleted
consent: (not asked yet)
cart: (empty)
user: ada