Server-side sessions
40 min · Intermediate
Need help? Ask a tutor on WhatsApp (opens in a new tab)A ShopLite cart can hold many items. Packing the whole cart into a cookie would make every request heavy, and the customer could edit it. The server needs to keep the cart itself and still know whose it is.
A session id in a cookie, the data on the server
A session is a record the server keeps for one logged-in user, such as the user name and cart. The browser holds only a long random session id in a cookie, and the server uses it to find the record.
Add two items to a cart with two requests, then read the cart back using only the cookie jar.
Example 1 · The cart lives on the server; the jar holds only an id
Runs in: the demo login server on localhost:8080
curl -c cookies.txt -d "username=asha" -d "password=campus123" http://localhost:8080/logincurl -b cookies.txt -d "item=Java notebook" http://localhost:8080/cartcurl -b cookies.txt -d "item=USB-C cable" http://localhost:8080/cartcurl -b cookies.txt http://localhost:8080/cartcat cookies.txt$ curl -c cookies.txt -d "username=asha" -d "password=campus123" http://localhost:8080/login
{"message":"Welcome, asha"}
$ curl -b cookies.txt -d "item=Java notebook" http://localhost:8080/cart
{"added":"Java notebook","cartItems":1}
$ curl -b cookies.txt -d "item=USB-C cable" http://localhost:8080/cart
{"added":"USB-C cable","cartItems":2}
$ curl -b cookies.txt http://localhost:8080/cart
{"user":"asha","items":["Java notebook","USB-C cable"]}
$ cat cookies.txt
# Netscape HTTP Cookie File
# https://curl.se/docs/http-cookies.html
# This file was generated by libcurl! Edit at your own risk.
#HttpOnly_localhost FALSE / TRUE 0 SID 569e86871970f18b9128db0ead9e883cWhat this code does
- Two items were added with two
POST /cartrequests, andGET /cartlists both. Each request carried only the sameSIDcookie. - The jar never changed: it still holds one random id. The cart itself is in the server's session store, under that id.
The records sit in a session store: a map from session ids to records, kept in memory, a database or a cache. The id is long and random, so nobody can guess another user's id.
Think of the cloakroom at a college fest: you keep a numbered token, and your bag stays at the counter. The token alone is worthless, and the counter cannot find your bag without it.
The login flow step by step
Log in with the right password, then with a wrong one, and see which response sets a cookie.
Example 2 · A login that works, and one that does not
Runs in: the demo login server on localhost:8080
curl -i -d "username=asha" -d "password=campus123" http://localhost:8080/logincurl -i -d "username=asha" -d "password=wrong" http://localhost:8080/login$ curl -i -d "username=asha" -d "password=campus123" http://localhost:8080/login
HTTP/1.1 200 OK
Content-Type: application/json
Content-Length: 28
Connection: close
Date: Fri, 16 Oct 2026 03:06:04 GMT
Set-Cookie: SID=325dff05935ad75c156d0a2dea217264; Path=/; HttpOnly; Secure; SameSite=Lax
{"message":"Welcome, asha"}
$ curl -i -d "username=asha" -d "password=wrong" http://localhost:8080/login
HTTP/1.1 401 Unauthorized
Content-Type: application/json
Content-Length: 39
Connection: close
Date: Fri, 16 Oct 2026 03:06:04 GMT
{"error":"Wrong username or password"}What this code does
- curl sent
Content-Type: application/x-www-form-urlencodedand the bodyusername=asha&password=campus123, the same format an HTML login form uses. - The right password creates a session and sets the
SIDcookie. The wrong one gets401and noSet-Cookie, so the client has nothing to send later.
A session login has one request that creates the session and many that use it:
- The browser sends the username and password in the body of
POST /login, over HTTPS. - The server checks the password against the stored password hash, a one-way scrambled form of the password.
- If it matches, the server creates a session with a new random id and stores
user = ashaunder that id. - The server replies with
Set-Cookie: SID=<id>; Path=/; HttpOnly; Secure; SameSite=Lax. - Every later request carries
Cookie: SID=<id>, and the server looks the id up to know the user.
If the password is wrong, the server replies 401 and sets no cookie.
Order of execution
Put the login flow in order
Tap the steps in the order they happen.
Logout and expiry: destroying the session
An idle timeout ends a session after a period with no requests, often 15 to 30 minutes, so a forgotten logged-in laptop does not stay open for ever.
Log out, then replay the old session id the way an attacker who copied it earlier would.
Example 3 · Logout destroys the session, so an old copy of the id is useless
Runs in: the demo login server on localhost:8080
curl -c cookies.txt -d "username=asha" -d "password=campus123" http://localhost:8080/logincat cookies.txtcurl -i -b cookies.txt -c cookies.txt -X POST http://localhost:8080/logoutcat cookies.txtcurl -i -b "SID=bfc5892b9961a9d537ff1a3430b64c98" http://localhost:8080/me$ curl -c cookies.txt -d "username=asha" -d "password=campus123" http://localhost:8080/login
{"message":"Welcome, asha"}
$ cat cookies.txt
# Netscape HTTP Cookie File
# https://curl.se/docs/http-cookies.html
# This file was generated by libcurl! Edit at your own risk.
#HttpOnly_localhost FALSE / TRUE 0 SID bfc5892b9961a9d537ff1a3430b64c98
$ curl -i -b cookies.txt -c cookies.txt -X POST http://localhost:8080/logout
HTTP/1.1 200 OK
Content-Type: application/json
Content-Length: 25
Connection: close
Date: Fri, 16 Oct 2026 02:38:56 GMT
Set-Cookie: SID=; Path=/; Max-Age=0
{"message":"Logged out"}
$ cat cookies.txt
# Netscape HTTP Cookie File
# https://curl.se/docs/http-cookies.html
# This file was generated by libcurl! Edit at your own risk.
$ curl -i -b "SID=bfc5892b9961a9d537ff1a3430b64c98" http://localhost:8080/me
HTTP/1.1 401 Unauthorized
Content-Type: application/json
Content-Length: 26
Connection: close
Date: Fri, 16 Oct 2026 02:38:58 GMT
{"error":"Not logged in"}What this code does
- Logout answered with
Set-Cookie: SID=; Path=/; Max-Age=0, and curl emptied the jar. - The last command plays an attacker who copied the id before logout. The server deleted the session record, so the old id gets
401. A logout that only clears the browser cookie would have let this request in.
Logout must delete the session on the server, not only the cookie in the browser. If the server keeps the record, anyone who copied the id earlier can still use it. The server also sends Set-Cookie: SID=; Max-Age=0 so the browser forgets the id.
An absolute timeout ends a session after a fixed time even if the user stays active, for example 8 hours. Banking and exam sites use short timeouts, because a forgotten logged-in laptop in a lab is a real risk.
Sessions with several servers: sticky sessions or a shared store
Sticky sessions are a load balancer setting that sends every request from one user to the same server copy, so a session kept in that copy's memory is always found.
Two copies keep sessions in their own memory: log in, ask again, and see which copy answers.
Example 4 · Two copies, sessions in memory: logged in, then logged out
Runs in: a demo load balancer on localhost:8000 in front of two copies of the demo server
curl -i -c cookies.txt -d "username=asha" -d "password=campus123" http://localhost:8000/logincurl -i -b cookies.txt http://localhost:8000/me$ curl -i -c cookies.txt -d "username=asha" -d "password=campus123" http://localhost:8000/login
HTTP/1.1 200 OK
Content-Type: application/json
Content-Length: 28
X-Served-By: server-b
Connection: close
Date: Fri, 16 Oct 2026 02:38:59 GMT
Set-Cookie: SID=5a815caa514905f7693b6c34b75cab69; Path=/; HttpOnly; Secure; SameSite=Lax
{"message":"Welcome, asha"}
$ curl -i -b cookies.txt http://localhost:8000/me
HTTP/1.1 401 Unauthorized
Content-Type: application/json
Content-Length: 26
X-Served-By: server-a
Connection: close
Date: Fri, 16 Oct 2026 02:39:01 GMT
{"error":"Not logged in"}What this code does
- The login went to
server-b, which stored the session in its own memory. The load balancer sent the next request toserver-a, which has never seen that id. - Same cookie, same user, opposite answers. With more copies and real traffic, users get logged out at random.
Example 5 · Two copies, one shared session store
Runs in: a demo load balancer on localhost:8000 in front of two copies of the demo server, sharing one session store
# Both copies now keep sessions in one shared store instead of their own memory.curl -i -c cookies.txt -d "username=asha" -d "password=campus123" http://localhost:8000/logincurl -i -b cookies.txt http://localhost:8000/me$ curl -i -c cookies.txt -d "username=asha" -d "password=campus123" http://localhost:8000/login
HTTP/1.1 200 OK
Content-Type: application/json
Content-Length: 28
X-Served-By: server-a
Connection: close
Date: Fri, 16 Oct 2026 02:39:11 GMT
Set-Cookie: SID=833cd0510bf4fd0071b17180ae9b94ef; Path=/; HttpOnly; Secure; SameSite=Lax
{"message":"Welcome, asha"}
$ curl -i -b cookies.txt http://localhost:8000/me
HTTP/1.1 200 OK
Content-Type: application/json
Content-Length: 30
X-Served-By: server-b
Connection: close
Date: Fri, 16 Oct 2026 02:39:12 GMT
{"user":"asha","cartItems":0}What this code does
server-acreated the session in the shared store, andserver-bfound it there on the next request.- In real apps the shared store is usually Redis, so every copy reads sessions quickly and a restart of one copy loses nothing.
When an app runs several copies behind a load balancer, a session kept in one copy's memory is invisible to the others. Sticky sessions are easy to switch on, but if that copy crashes its users are logged out, and busy users can overload one copy.
With a shared session store, every copy keeps sessions in one place that all of them can reach, usually Redis (a fast in-memory data store) or a database. Any copy can then answer any request, which is the usual choice.
Predict the output
Who knows the session?
Pick the status of the last request. Two copies, A and B, answer in turn unless stated.
Round 1 of 3
Each copy keeps sessions in its own memory.
Risks: hijacking, fixation and CSRF, and their fixes
Session hijacking is an attack in which someone steals a valid session id and uses it to act as that user.
Compare the session id before and after login to see how a fresh id stops a planted one.
Example 6 · A new session id at login stops fixation
Runs in: the demo login server on localhost:8080
curl -i -c cookies.txt http://localhost:8080/homecurl -i -b cookies.txt -c cookies.txt -d "username=asha" -d "password=campus123" http://localhost:8080/logincurl -b "SID=a4759f93fa42173712236e2c167cd7a6" http://localhost:8080/mecurl -b cookies.txt http://localhost:8080/me$ curl -i -c cookies.txt http://localhost:8080/home
HTTP/1.1 200 OK
Content-Type: application/json
Content-Length: 29
Connection: close
Date: Fri, 16 Oct 2026 02:39:12 GMT
Set-Cookie: SID=a4759f93fa42173712236e2c167cd7a6; Path=/; HttpOnly; Secure; SameSite=Lax
{"message":"Hello, visitor"}
$ curl -i -b cookies.txt -c cookies.txt -d "username=asha" -d "password=campus123" http://localhost:8080/login
HTTP/1.1 200 OK
Content-Type: application/json
Content-Length: 28
Connection: close
Date: Fri, 16 Oct 2026 02:39:13 GMT
Set-Cookie: SID=c4de36bb831a7fb60d6124a42998e3f7; Path=/; HttpOnly; Secure; SameSite=Lax
{"message":"Welcome, asha"}
$ curl -b "SID=a4759f93fa42173712236e2c167cd7a6" http://localhost:8080/me
{"error":"Not logged in"}
$ curl -b cookies.txt http://localhost:8080/me
{"user":"asha","cartItems":0}What this code does
- A visitor got a session (
a4759f93...) before logging in. At login the server deleted it and issued a different id (c4de36bb...). - If an attacker had planted the first id, it now points to nothing: only the new id, which the attacker never saw, is logged in.
Whoever holds a session id is treated as that user, so most attacks try to steal, plant or misuse the id:
- Session hijacking: the id is stolen from plain HTTP traffic, an injected script or a log file. Fix: HTTPS everywhere,
SecureandHttpOnlycookies, long random ids and short timeouts. - Session fixation: the attacker makes the victim use an id the attacker already knows, then waits for the login. Fix: always issue a brand-new id at login and throw the old one away.
- CSRF (cross-site request forgery): a page on another site makes your browser send a request to the real site, and the browser attaches your cookie. Fix:
SameSite=LaxorStrict, plus a CSRF token, a random value the real site puts in its own forms and checks on every change. - Guessing: short or predictable ids can be tried one by one. Fix: at least 128 random bits from a secure random generator, like the 32 hex characters in the examples.
Find the bug
Find the session bug
Click the line that makes the code unsafe.
Which line lets an attacker who planted a session id take over the login?
Round 1 of 3Next step
Check your understanding
12 quick questions