Skip to content

Core\Session::get

Core\Session::get() returns the value saved under one key in the session of the current request. It reads a value saved with Core\Session::setShortLived() or Core\Session::setLongLived().

If the session has no value under the key, get() returns null. A key set to null gives the same result. A short-lived value is also null after the visitor closes the browser.

The return type is mixed. Use as to convert the value, for example as ?int. Write the default before the conversion, because null as string is an empty string.

The first call to any Core\Session method opens the session. Reading does not change the session, so nothing is written to the store.

get() throws a LogicError when the program is not handling a request. It throws a RuntimeError when the store cannot be opened, and an IOError when the store cannot be reached.

The examples below show how to read a value, what a missing key returns, and a page view counter.

Parameters

  • $keystringThe key to read. If the session has no value under it, the result is null. A key that was set to null gives the same result.

Return value

Returns mixed. The value saved under $key, or null. A short-lived value is null after the browser was closed, or after the visitor was inactive for [session] short_lived.

Errors

  • LogicErrorThe program is not handling a request. A command-line program, a job and a test have no session.
  • 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] url is not set, or the program does not have the cache.shared capability.
  • IOErrorThe store cannot be reached. The user may still have a session there, so do not treat them as signed out.
  • ParseErrorThe value under $key contains an object of a class this program does not declare. This happens when another program saved it.

Changelog

VersionChange
0.0.5Core\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.5Core\Session::get inside Core\Html::later returns the value saved in the session of the request. This also works when the main part of the request did not use the session.

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"

Read a saved value

<?nvs
// Shows how to read values that were saved in the session.
// requires: redis
Core\Session::setShortLived('user', 'ada');
Core\Session::setLongLived('language', 'de');
// `get()` reads a value of either lifetime. This prints `ada` and then `de`.
echo 'user: ', Core\Session::get('user') ?? '(none)', "\n";
echo 'language: ', Core\Session::get('language') ?? 'en', "\n";
// No value was saved under "currency", so `??` gives the default "EUR".
// Write `??` before `as string`, because `null as string` is an empty string.
tainted string $currency = (Core\Session::get('currency') ?? 'EUR') as string;
echo 'currency: ', $currency, "\n";
Output
user: ada
language: de
currency: EUR

A missing key is null

<?nvs
// Shows that a missing key and a key set to `null` give the same result.
// requires: redis
Core\Session::setLongLived('coupon', null);
// Both lines print `(none)`, because `get()` returns `null` for both keys.
echo 'coupon: ', Core\Session::get('coupon') ?? '(none)', "\n";
echo 'gift: ', Core\Session::get('gift') ?? '(none)', "\n";
// To delete a key, use `remove()`. After it, `get()` also returns `null`.
Core\Session::setShortLived('gift', 'card');
Core\Session::remove('gift');
echo 'gift: ', Core\Session::get('gift') ?? '(none)', "\n";
Output
coupon: (none)
gift: (none)
gift: (none)

Count page views

<?nvs
// Shows a counter of how many pages a visitor has opened since the browser started.
// requires: redis
// On the first page there is no saved count, so `get()` returns `null`
// and the count starts at 0. `as ?int` converts the saved value to a whole number.
int $views = Core\Session::get('views') as ?int ?? 0;
$views = $views + 1;
// A short-lived value is deleted when the visitor closes the browser.
// So the count starts again at 0 in a new browser session.
Core\Session::setShortLived('views', $views);
// This is the visitor's first request, so it prints `1`.
echo 'pages opened: ', $views, "\n";
Output
pages opened: 1