Cookie-Based Authentication
What you'll learn: How cookies store session identifiers and automatically travel with every browser request to maintain authenticated state.
How It Works
When you log into a website, the server creates a session on its side and generates a unique session identifier (often called a session ID). The server then sends this ID back to your browser inside a cookie—a small piece of data that the browser stores.
Here's the key mechanism: every time your browser makes a subsequent request to that same domain, it automatically attaches all relevant cookies to the request headers. The server reads the session ID from the cookie, looks up your session data (who you are, what permissions you have), and knows you're authenticated—without you having to log in again.
The Automatic Cookie Behavior
Think of cookies like a backstage pass at a concert. Once security gives you the pass (the server sets the cookie), you wear it around your neck. Every time you approach a restricted area (make a request), security sees your pass automatically—you don't have to show ID again.
The browser handles this entirely behind the scenes. When the server responds with a Set-Cookie header, the browser saves it. On every subsequent request to that domain, the browser includes a Cookie header with the session ID.
Why This Matters
This automatic behavior is what makes cookie-based authentication both convenient and vulnerable. It's convenient because users stay logged in across page loads. It's vulnerable because if an attacker tricks your browser into making a request (remember CSRF?), those cookies go along for the ride automatically—potentially performing actions as you.
Key Takeaway: Cookie-based authentication stores session identifiers in cookies that browsers automatically include with every request to the domain, enabling stateful authentication without re-entering credentials.