Identity and access Sessions 4 min read
What is a session?
HTTP forgets you the moment each request ends. So after you sign in once, how does a site know the next click is still you?
-
Sign in once, then the cookie does the work
When your password checks out, the server makes up a long random ID and writes down whose it is in a session store, such as a database or cache.
It hands that ID to your browser as a cookie. From then on your browser includes it with every request to the site, and the server looks it up to find you. No password needed.
You sign in and the server checks your password once. It records that session 8f3a91c2 is Priya in the session store, and sends the ID back as a cookie. On the next request the browser includes the cookie, the server looks it up, and finds Priya.
The password crosses once. The session ID crosses every time. In short The session ID in the cookie stands in for you on every request.
-
The settings that guard it
Anyone holding that cookie is you, as far as the site knows, so it comes with settings that limit where it can go:
Set-Cookie: session=8f3a91c2; HttpOnly; Secure; SameSite=Lax.HttpOnlyhides it from JavaScript on the page, even injected JavaScript.Securemeans it only travels over HTTPS.SameSite=Laxkeeps it off most requests other sites trigger. A short expiry limits how long any copy stays useful.In short Each setting closes one way the cookie could leak or be misused.
-
Signing out has to happen on the server
Your cookie is only a copy of the ID. If “Sign out” just deletes it from your browser, the session is still alive on the server.
Any other copy, from a shared computer or a stolen cookie, keeps working until the server ends the session.
Your browser deletes its cookie, but the server only removes the session from the store when sign-out reaches it. After that, a request with an old copy of the cookie finds nothing and is refused.
A real sign-out removes the session, so every copy stops working. In short Deleting your copy isn’t signing out. Ending the session on the server is.
What a session doesn’t do
A session proves a request carries the right ID, not that the right person sent it. Anyone who gets the cookie is signed in as you until the session ends.
It also doesn’t stop the site’s own pages from being turned against you. A forged form on another site can still ride your session unless SameSite or CSRF checks are in place.
Keep this
The session cookie stands in for you. Protect it, and end it on the server.
HttpOnly hides it from page scripts, Secure keeps it on HTTPS, SameSite keeps it off other sites’ requests, and sign-out must end it server-side.- Session ID
- A random value the server links to you.
- Cookie
- The browser includes it with every request to that site.
- Settings
- HttpOnly, Secure and SameSite limit where it can go.
- Sign-out
- Must end the session on the server, not just delete your copy.
Questions worth carrying
Sessions or JWTs?
Both say “this is Priya”. A session keeps the details on the server, so it’s easy to end. A JWT carries them in the token, so it’s hard to end early.
Why change the session ID at sign-in?
If someone planted a session ID before you signed in, issuing a fresh one at sign-in makes the planted one useless.