Sessions and cookies are both used to track user state (like being logged in) across multiple HTTP requests since HTTP itself is stateless. Here’s how they work and how they differ:


1. Cookies

Typical Flow:

  1. User logs in → Server responds with a Set-Cookie header.
  2. The browser saves this cookie.
  3. For subsequent requests, the browser automatically sends the cookie in the Cookie header.

Example of a cookie in HTTP:

Set-Cookie: session_id=abcd1234; HttpOnly; Secure; SameSite=Strict;

Cookies can store:


2. Sessions