Skip to content

Core\Session::regenerate

Core\Session::regenerate(): void

Core\Session::regenerate() gives the session of the current request a new identifier. The identifier is the value in the session cookie. Call it when a user signs in. Somebody else may have known the old identifier. After regenerate(), the old identifier no longer finds the session.

Every value is kept: short-lived values, long-lived values and secrets of both lifetimes. A value that another request saved in the meantime is kept too. The response sends the new identifier in the session cookie. A session with nothing saved has no identifier, so regenerate() does nothing.

To sign a user out, use Core\Session::clearShortLived(). It also gives a new identifier, but it deletes the short-lived values.

Outside a request, regenerate() throws a LogicError. Inside Core\Html::later or Core\Task::runAfterResponse the call does not compile or throws a LogicError. The session store is the place where the server keeps session data. A store that cannot be opened throws a RuntimeError, and a store that cannot be reached throws an IOError.

The examples below show a new identifier after sign-in, that values and secrets are kept, and a sign-in page.

Return value

Returns void. Nothing. Every value in the session is kept, also a value that another request saved in the meantime. The response sends the new identifier in the session cookie. The old identifier no longer finds the session.

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::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.5The 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.

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"

A new identifier after sign in

<?nvs
// Shows how to give the session a new identifier when a user signs in.
// requires: redis
// The visitor chose a language before signing in.
Core\Session::setLongLived('language', 'de');
// The user signed in. `regenerate()` replaces the identifier in the session cookie.
Core\Session::regenerate();
Core\Session::setShortLived('user', 'ada');
// The session works as before. This prints `signed in as ada`.
echo 'signed in as ', Core\Session::get('user') ?? '(nobody)', "\n";
Output
signed in as ada

Values and secrets are kept

<?nvs
// Shows that `regenerate()` keeps every value and every secret in the session.
// requires: redis
secret bytes $key = Core\Crypto::generateKey();
array<secret bytes> $keys = [$key];
// One value of each lifetime, and one secret.
Core\Session::setLongLived('language', 'de');
Core\Session::setShortLived('user', 'ada');
secret string $token = 'rt-51d0e8aa';
Core\Session::setShortLivedSecret('refresh-token', $token, $keys);
Core\Session::regenerate();
// All three are still there. This prints `language: de`, `user: ada` and `token: kept`.
echo 'language: ', Core\Session::get('language') ?? '(none)', "\n";
echo 'user: ', Core\Session::get('user') ?? '(nobody)', "\n";
echo 'token: ',
(Core\Session::getSecret('refresh-token', $keys) ?? '') == $token ? 'kept' : 'lost', "\n";
Output
language: de
user: ada
token: kept

A sign in page

<?nvs
// Shows a sign-in page that gives the session a new identifier only when the password is right.
// requires: redis
class Login
{
public static function signIn(string $user, string $password): string
{
// A real program checks the password against its database here.
if ($password != 'correct horse') {
return 'wrong password';
}
// The user is now signed in, so the session gets a new identifier first.
Core\Session::regenerate();
Core\Session::setShortLived('user', $user);
return 'welcome, ' . $user;
}
}
// This prints `wrong password`. The session is not changed.
echo Login::signIn('ada', 'guess'), "\n";
// This prints `welcome, ada`.
echo Login::signIn('ada', 'correct horse'), "\n";
echo 'user: ', Core\Session::get('user') ?? '(nobody)', "\n";
Output
wrong password
welcome, ada
user: ada