Session
Management
Understand how web applications track users across requests using cookies, tokens, and session IDs: the invisible glue of the modern web.
What Is a Session?
HTTP is a stateless protocol: each request the browser sends is independent, with no built-in memory of previous interactions. A session is the mechanism that lets a server remember who you are across multiple requests, from login to logout.
Why Sessions Matter
The Session Lifecycle
What Are Cookies?
A cookie is a small piece of data (up to ~4 KB) that a server sends to a browser via the Set-Cookie HTTP header. The browser stores it and automatically attaches it to every subsequent request to that domain.
Cookie Security Flags
What Are Tokens?
A token is a self-contained credential, often a JSON Web Token (JWT), that encodes user identity and claims. Unlike cookies, tokens are typically stored in application memory or localStorage and sent via the Authorization header.
Cookies vs Tokens
| Cookies | Tokens (JWT) | |
|---|---|---|
| Storage | Browser cookie jar | localStorage / memory |
| Sent via | Automatic (Cookie header) | Manual (Authorization header) |
| State | Server-side (session store) | Stateless (self-contained) |
| Scalability | Requires shared store | Easily scaled horizontally |
| CSRF Risk | Higher (auto-sent) | Lower (manual attach) |
| XSS Risk | Lower (HttpOnly flag) | Higher (if in localStorage) |
Session ID Deep Dive
A session ID is a long, random, unpredictable string generated by the server. It acts as a lookup key into a server-side session store (database, Redis, or memory) that holds the actual user data.
How to Secure a Session
Example: Session Fixation
Example: Session Hijacking
Example: CSRF via Sessions
Token Best Practices
Common Mistakes
Session Management Summary
| Concept | Purpose | Key Defence |
|---|---|---|
| Session ID | Identifies user across requests | High entropy + regeneration |
| Cookies | Transport session ID automatically | HttpOnly, Secure, SameSite |
| Tokens (JWT) | Stateless authentication | Short expiry + signature check |
| Session Fixation | Attack: force known ID | Regenerate ID on login |
| Session Hijacking | Attack: steal valid ID | HTTPS + Secure flag |
| CSRF | Attack: forge requests | SameSite + CSRF tokens |
Question 1 of 5
HTTP is described as a "stateless" protocol. What does this mean for session management?
Question 2 of 5
Which cookie flag prevents JavaScript from accessing the cookie, thereby mitigating XSS-based session theft?
document.cookie) from reading or modifying the cookie. Secure restricts to HTTPS, and SameSite controls cross-origin sending.Question 3 of 5
An attacker sends a victim a link containing a pre-set session ID. After the victim logs in, the attacker uses that same ID to access the victim's account. Which attack is this?
Question 4 of 5
A JWT (JSON Web Token) consists of three parts. Which part ensures the token has not been tampered with?
Question 5 of 5
CSRF attacks exploit cookies because cookies are sent automatically with every request. Which combination of defences best mitigates CSRF?