Explanation
What Is Sessions
HTTP Sessions
HTTP is a stateless protocol. Each request is independent, and HTTP itself provides no built-in mechanism for the server to remember that two requests came from the same user.
For example:
Request 1:
POST /login
→ username + password → authentication succeeds
Request 2:
GET /dashboard
→ how does the server know this is the user who just logged in?Without some additional mechanism, the server has no inherent connection between those two requests.
A session solves this by giving the application a way to associate multiple HTTP requests with the same ongoing interaction:
Login
↓
Server creates session
↓
Client receives session identifier
↓
GET /dashboard
→ session identifier included
↓
Server finds corresponding session
↓
"These requests belong to the same authenticated user"The session can maintain state such as:
user_id → 4521
authenticated → true
cart_id → 8831
checkout_step → paymentThe important distinction is:
HTTP itself
→ stateless
Application session
→ state maintained across requestsA session therefore doesn't change HTTP's underlying stateless nature. It adds an application-level mechanism for continuity on top of it.
In a typical web application, the client stores only a session identifier, commonly in a cookie:
Cookie:
session_id=abc123...The actual session state can remain server-side:
session_id abc123...
↓
server-side session
↓
user_id = 4521
authenticated = trueThat separation is important for security: the client presents an identifier, while the server determines what that identifier represents.