Core\Session::remove
Core\Session::remove(string $key): voidCore\Session::remove( string $key, ): void
Core\Session::remove() deletes one key and its value from the session of the current request. After it, Core\Session::get() returns null for that key. The other keys keep their values. One remove() deletes a value of either lifetime, short-lived or long-lived.
Removing a key that the session does not have is allowed. It does not throw an error and does not change the session.
You do not need to open the session first. The first call to any Core\Session method opens it. The changed session is saved to the session store when the request ends. The session store is the place where the server keeps session data.
remove() throws a LogicError when the program is not handling a request, for example a command-line program. It also throws a LogicError inside Core\Html::later and Core\Task::runAfterResponse, because the response headers are already sent there. It throws a RuntimeError when the session store cannot be opened, and an IOError when the store cannot be reached.
Good to know: to delete many values at once, use Core\Session::clearShortLived(), Core\Session::clearLongLived() or Core\Session::clearAll().
The examples below show how to remove one value, what happens when the key is missing, and a message that a page shows only once.
Parameters
$keystringThe key to delete. If the session has no value under it,remove()does not throw an error and the session does not change.
Return value
Returns void. Nothing. After it, get() returns null for $key. The other keys keep their values.
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.
Changelog
| Version | Change |
|---|---|
0.0.5 | Core\Session::get, Core\Session::remove and Core\Session::regenerate open the session on their first use. In a program that handles no request, they throw a LogicError. Core\Session::get returns null for a short-lived value after the browser closes. |
0.0.5 | The session saves only the keys that a request changed. Two requests that change different keys at the same time keep both changes. Core\Session::regenerate keeps every value, also a value that another request saved while it ran. |
0.0.5 | Core\Session::remove inside Core\Html::later or Core\Task::afterResponse does not compile. Where the compiler cannot see the call, it throws a LogicError. |
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"Remove one value
<?nvs// Shows how to delete one value from the session and keep the others.// requires: redis
Core\Session::setShortLived('user', 'ada');Core\Session::setLongLived('cart', 3);
// `remove()` deletes the key "cart" and its value.// It works the same for a short-lived and a long-lived value.Core\Session::remove('cart');
// "cart" is gone, so this prints `(none)`. "user" still has its value `ada`.echo 'cart: ', Core\Session::get('cart') ?? '(none)', "\n";echo 'user: ', Core\Session::get('user') ?? '(none)', "\n";cart: (none)user: adaA missing key is allowed
<?nvs// Shows that removing a key the session does not have is allowed.// requires: redis
Core\Session::setLongLived('language', 'de');
// Nothing was saved under "coupon". `remove()` does not throw an error,// and the session does not change.Core\Session::remove('coupon');echo 'coupon: ', Core\Session::get('coupon') ?? '(none)', "\n";echo 'language: ', Core\Session::get('language') ?? '(none)', "\n";
// Removing the same key twice is also allowed. This prints `(none)`.Core\Session::remove('language');Core\Session::remove('language');echo 'language: ', Core\Session::get('language') ?? '(none)', "\n";coupon: (none)language: delanguage: (none)Show a message once
<?nvs// Shows a message that a page displays one time, for example after a form is saved.// requires: redis
class Notice{ // Reads the message and deletes it, so the next page does not show it again. public static function take(): tainted string { tainted string $text = (Core\Session::get('notice') ?? '') as string; Core\Session::remove('notice'); return $text; }}
// The form handler saves the message.Core\Session::setShortLived('notice', 'Your address was saved.');
// The first call returns the message. The second call returns an empty// string, because `remove()` deleted the message.echo 'first: ', Notice::take(), "\n";echo 'second: ', Notice::take(), "\n";first: Your address was saved.second: