The session
$session keeps values between requests for the same browser. It is kept on the server, and the browser only holds a random id in a cookie.
A session that holds nothing yet is not kept anywhere. A visitor who only loads a page with a form or a live component gets the cookie and a CSRF token made from its id, and the server writes its first record when something is set in the session or someone signs in. So a crowd of visitors, or a script loading a page over and over, can't fill the session store. The id is signed with KSCRIPT_SECRET, and an id the app didn't make is not a session.
<import package="KScript.Auth" />
<do>$session->set('theme', 'dark')</do>
<let name="theme" value="$session->get('theme', 'light')" />
<do>$session->flash('status', 'Your profile was saved.')</do>import KScript.Auth
$session->set('theme', 'dark')
$theme = $session->get('theme', 'light')
$session->flash('status', 'Your profile was saved.')| Method | What it does |
|---|---|
->get(key, default?) |
The value, or the default. |
->has(key) |
True when the key is there. |
->set(key, value) |
Keeps a value. Values are kept as JSON, and a password is refused. |
->flash(key, value) |
Keeps a value for the next request only: a message to show after a redirect. |
->remove(key) |
Removes a key. True if it was there. |
->pull(key, default?) |
The value, which is then removed: for a one-time code or message. |
->clear() |
Removes every value, keeping the session and who is signed in. Returns how many there were. |
->all() |
Every value, as a map. |
->regenerate() |
Gives the session a new id with the same values. @login does this for you. |
.token |
The CSRF token, like @csrf_token(). |
Keys starting with _ are kept by KScript.Auth, so they can't be set. The cookie is HttpOnly and SameSite=Lax. It is Secure on https, behind a proxy that sends X-Forwarded-Proto: https, or with SameSite=None. A session lasts http.auth.lifetime (2 hours) after the last request. Sessions and reset tokens that have run out are removed from the state folder now and then, as new ones are written.