What Are Sessions?
Every time your browser makes a request to a server, that request arrives with no memory of previous interactions. HTTP is stateless — each request is independent. So how does a website know you're the same person who logged in thirty seconds ago?
Sessions solve this problem by creating a persistent identity across multiple requests.
How Sessions Work
When you first interact with a website, the server creates a session — a temporary record that represents your visit. The server generates a unique session ID (a long, random string) and sends it to your browser, usually stored in a cookie.
From that point forward, every request your browser makes includes that session ID. The server looks up the ID, finds your session data, and knows exactly who you are and what you've been doing.
Think of it like getting a table number at a restaurant. When you sit down, the host gives you number 47. Every time a server brings food, they check the number — they don't need to ask your name repeatedly. The number connects all your orders to your table.
What Sessions Store
Sessions typically hold information like:
- Whether you're logged in
- Your user ID or account details
- Items in your shopping cart
- Preferences you've selected during your visit
This data lives on the server, not in your browser. Your browser only holds the session ID — the key that unlocks your information.
Session Lifecycle
Sessions don't last forever. They expire after a period of inactivity (often 15-30 minutes) or when you explicitly log out. When a session expires, the server deletes the stored data, and your next visit starts fresh.
This expiration is intentional. If you walk away from a public computer, you don't want someone else accessing your account hours later.
Why This Matters
Sessions enable the personalized, continuous experiences we expect from modern websites. Without them, you'd need to log in on every single page, and shopping carts would empty between clicks.
Understanding sessions helps you grasp how authentication works and why certain actions (like clearing cookies) log you out of websites.