Skip to content

Core\Session::openWithIdentifier

Core\Session::openWithIdentifier(string $id): voidCore\Session::openWithIdentifier(
    string $id,
): void

Core\Session::openWithIdentifier($id) opens the session of the current request under an identifier that your program read itself. A mobile app, for example, may send it in a header. Most programs do not need this method: the first call of any other Core\Session method opens the session from the session cookie.

openWithIdentifier() reads no cookie and sends no cookie. A short-lived value in this session needs only the identifier, because there is no browser that could close.

If the store has no session under $id, the result is a new, empty session. This happens when the identifier expired, and when somebody made it up.

It must be the first Core\Session call in the request. Otherwise it throws a LogicError that names the method that opened the session. It also throws a LogicError outside a request. It throws a RuntimeError if the store cannot be opened, and an IOError if it cannot be reached.

The examples below show an identifier sent in a header, a call that comes too late, and a page for both an app and a browser.

Parameters

  • $idstringThe session identifier. If the store has no session under it, a new, empty session is opened. This happens when the identifier expired, and when somebody made it up.

Return value

Returns void. Nothing. No cookie is read for this session. Short-lived values need only the identifier, because there is no browser that could close.

Errors

  • LogicErrorAnother method of this class was already called in this request, so the session is already open. The message names that method. It is also thrown when the program is not handling a request.
  • 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.

Changelog

VersionChange
0.0.5Core\Session::openWithIdentifier opens the session under an identifier your program read itself, for example from a header. It reads no cookie and sends no cookie. It must be the first Core\Session call of the request.

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"

An identifier in a header

<?nvs
// Shows how to open a session when the client sends its identifier in a header, not in a cookie.
// requires: redis
// A mobile app sends the identifier in the `X-Session` header.
// `openWithIdentifier()` opens the session with that identifier.
Core\Session::openWithIdentifier(Core\Request::header('X-Session') ?? '');
// The store has no session under this identifier. Somebody made it up.
// So the session is new and empty, and this prints `user: (nobody)`.
echo 'user: ', Core\Session::get('user') ?? '(nobody)', "\n";
Output
user: (nobody)

Called too late

<?nvs
// Shows that `openWithIdentifier()` must be the first call of `Core\Session` in a request.
// requires: redis
// `get()` opens the session from the session cookie.
echo 'theme: ', Core\Session::get('theme') ?? '(default)', "\n";
// The session is already open, so `openWithIdentifier()` throws a `LogicError`.
// The message names `get()`, the method that opened the session.
try {
Core\Session::openWithIdentifier('an-identifier-from-a-header');
} catch (LogicError $e) {
echo $e->message, "\n";
}
Output
theme: (default)
Core\Session::openWithIdentifier(): the session of this request is already open. `Core\Session::get()` opened it. Call `openWithIdentifier()` before any other `Core\Session` method.

A header or a cookie

<?nvs
// Shows a page that serves both a mobile app and a browser.
// requires: redis
// The app sends the identifier in the `X-Session` header. A browser sends a cookie.
// Only a request with the header needs `openWithIdentifier()`.
?tainted string $id = Core\Request::header('X-Session');
if ($id != null) {
Core\Session::openWithIdentifier($id);
}
// This request came from a browser. So `setShortLived()` opens the session from the cookie.
Core\Session::setShortLived('last-page', '/orders');
// This prints `last page: /orders`.
echo 'last page: ', Core\Session::get('last-page') ?? '(none)', "\n";
Output
last page: /orders