Home › Other › Cookies

Cookies

HTTP forgets you the instant a request ends. Cookies are the small notes that let a site remember who you are.

HTTP is stateless — each request stands alone, and the server has no built-in memory of the last one. Cookies fix that: the server hands the browser a small piece of data, and the browser sends it back on every subsequent request.

How they are set

The server includes a header in its response:

Set-Cookie: session=abc123; HttpOnly; Secure; SameSite=Lax; Max-Age=3600

From then on the browser automatically attaches it:

Cookie: session=abc123

The important attributes

AttributeEffect
HttpOnlyJavaScript cannot read it — blocks theft via XSS.
SecureOnly sent over HTTPS.
SameSiteControls whether it is sent on cross-site requests — the main CSRF defense.
Max-Age / ExpiresHow long it lives. Without them it is a “session” cookie and vanishes when the browser closes.
Domain / PathWhich requests the cookie is attached to.

Sessions

A login usually works like this: you sign in, the server creates a session and stores it, and gives you a cookie holding only an opaque session ID. The real data stays on the server; the cookie is just the claim ticket. Because it identifies you, a stolen session cookie is as good as your password — which is why HttpOnly and Secure matter.

Cookies, localStorage, and tokens

Privacy note: “third-party” cookies — set by domains other than the one in the address bar — are what powered cross-site tracking, and browsers are busy phasing them out.

Keep reading