HTTP is stateless by design. Each request from a browser to a server is completely independent. The server has no built-in memory of previous requests from the same client.
This creates a challenge: How do we build applications where users can log in, maintain shopping carts, or have personalized experiences?
We have three fundamental options for maintaining state:
Data stored in the browser and sent with requests:
?user=alice&page=2)Data stored on the server, indexed by some identifier:
A small token stored client-side that points to server-side data:
The cookie holds only an identifier (a few bytes). All actual user data stays safely on the server.
| Mechanism | Location | Persistence | Size Limit | Security |
|---|---|---|---|---|
| URL Parameters | URL (visible) | Per-request only | ~2KB | Visible in logs, history, referrer headers |
| Hidden Form Fields | HTML form | Form submission only | Unlimited | User can view/modify via DevTools |
| Cookies | Browser | Configurable (session or persistent) | ~4KB per cookie | Sent with every request; can be HTTP-only |
| localStorage | Browser | Permanent (until cleared) | ~5MB | JavaScript access only; same-origin policy |
| sessionStorage | Browser | Tab lifetime | ~5MB | JavaScript access only; per-tab isolation |
| Server Sessions | Server | Configurable | Unlimited | Only session ID exposed to client |
| JWT | Client (token) | Token lifetime | ~8KB practical | Signed but not encrypted; depends on storage |
Best for: Shareable, bookmarkable state
/search?q=web+development/products?page=3/catalog?category=books&sort=priceAvoid for: Sensitive data (passwords, tokens), large data sets
Best for: Data that must be sent to the server automatically
Avoid for: Large data (sent with every request)
Best for: Persistent client-side data not needed by server
Avoid for: Sensitive data (accessible to any JavaScript on the page)
Best for: Temporary state within a single tab
Avoid for: Data needed across tabs or after browser closes
Best for: Secure user-specific data
Avoid for: Static data that doesn't need protection
Explore each state mechanism with hands-on examples:
| Mechanism | Risks | Mitigations |
|---|---|---|
| URL Parameters | Logged in server logs, browser history, referrer headers | Never put sensitive data in URLs |
| Cookies | XSS can steal cookies; CSRF can abuse them | HttpOnly flag, Secure flag, SameSite attribute |
| localStorage/sessionStorage | Any JavaScript can read it (XSS vulnerability) | Never store sensitive tokens; sanitize all inputs |
| Server Sessions | Session fixation, session hijacking | Regenerate session ID on login; use HTTPS |
| Hidden Form Fields | User can modify values via DevTools | Never trust for authorization; validate server-side |
| JWT | XSS if in localStorage; difficult to revoke | Store in httpOnly cookie; use short expiry + refresh tokens |