Skip to content

Core\Session::clearAll

Core\Session::clearAll(): void

Core\Session::clearAll() deletes the whole session of the current request at once. It deletes it from the session store, the place where the server keeps session data. The response tells the browser to delete both session cookies.

After it, Core\Session::get() returns null for every key. You can save values again in the same request. The first new value starts a new session with a new identifier.

A short-lived value, such as a sign-in, ends when the visitor closes the browser. A long-lived value, such as a language choice, stays for 180 days by default. clearAll() deletes both kinds. To sign a user out and keep their choices, use Core\Session::clearShortLived().

clearAll() throws a LogicError in a program that is not answering a request. It also throws one inside Core\Html::later and Core\Task::runAfterResponse, where most such calls do not compile. If the store cannot be reached, it throws an IOError, and the data stays in the store.

The examples below show a session with both kinds of values deleted, and a session used again after clearAll(). The last one is a page that deletes a visitor's data.

Return value

Returns void. Nothing. After it, the request has an empty session with no identifier. If the request saves a value after clearAll(), a new session starts with a new identifier.

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 data is still in the store, and the session stays open, so you can try again.

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 this browser

<?nvs
// Shows how to delete every value of a session, the short-lived and the long-lived ones.
// requires: redis
// The user signed in, and chose a language for the site.
Core\Session::setShortLived('user', 'ada');
Core\Session::setLongLived('language', 'en');
echo 'before: ', Core\Session::get('user') ?? '(nobody)', ', ', Core\Session::get('language') ?? '(none)', "\n";
// `clearAll()` deletes the session from the store, and tells the browser to delete its cookies.
Core\Session::clearAll();
// Both values are gone. This prints `after: (nobody), (none)`.
echo 'after: ', Core\Session::get('user') ?? '(nobody)', ', ', Core\Session::get('language') ?? '(none)', "\n";
Output
before: ada, en
after: (nobody), (none)

A new session after it

<?nvs
// Shows that the session can be used again in the same request after `clearAll()`.
// requires: redis
Core\Session::setLongLived('cart', 3);
Core\Session::clearAll();
// The session is empty now. `get()` returns `null`, and this prints `cart: (empty)`.
echo 'cart: ', Core\Session::get('cart') ?? '(empty)', "\n";
// A new value starts a new session with a new identifier. This prints `cart: 1`.
Core\Session::setLongLived('cart', 1);
echo 'cart: ', Core\Session::get('cart') ?? '(empty)', "\n";
Output
cart: (empty)
cart: 1

A delete my data page

<?nvs
// Shows a page that deletes everything the site saved about a visitor.
// requires: redis
// The sign-in page saved the user's name, and the cookie banner saved a choice.
// These two lines do the same, so the example has data to delete.
Core\Session::setShortLived('name', 'Ada');
Core\Session::setLongLived('consent', 'all');
// The program reads the name first, because `clearAll()` deletes it.
if (Core\Request::path() == '/privacy/delete-my-data') {
tainted string $name = Core\Session::get('name') as ?string ?? 'guest';
Core\Session::clearAll();
echo 'Goodbye, ', $name, ". Your data is deleted.\n";
}
Output
Goodbye, Ada. Your data is deleted.