Your browser shows the same ID card on every request. Here is how cookies, sessions and tokens work, and why logout is the hard part.
You log into a website once, and it remembers you for weeks. You do not type your password every time you click. But the web, by default, forgets you right away.
So how does a website know it is still you? Your browser carries one small piece of text, like an ID card. Staying logged in is just your browser showing the same ID again and again, and the server believing it.
HTTP, the language browsers and servers use to talk, is mostly stateless. That means every request is a fresh start, like a shop assistant who forgets every customer the moment they walk away. You log in, click "Profile", and the server has no idea you just logged in.
RFC 6265, the official rulebook for cookies, states they let servers keep "a stateful session over the mostly stateless HTTP protocol". That is the whole job.
So how does the ID card get issued?
A cookie is a small bit of data a server gives your browser. The browser stores it and sends it back on later requests to that same server.
Click Next and follow the login, then the next request.
You send your username and password
The browser decides whether to send the cookie, based on the cookie's own rules. It is not added to everything.
The cookie works either way. The real question is what it contains.
The cookie can hold a random label, or it can hold the whole answer.
The cookie is a random, meaningless ID like a3fWa. The server keeps a table: this ID means one particular user, logged in. Delete the row and that user is logged out at once.
The cookie carries the facts itself: who you are, when it expires, plus a signature so nobody can fake it. The server stores nothing. It just checks the signature.
OWASP's session guide states a session ID should be meaningless, just an identifier, with the real info (user, role, timeouts) kept on the server. For IDs you make yourself, it suggests at least 128 bits of randomness, so guessing one is hopeless.
Either way, that ID card is now valuable. So how do you keep it safe?
A cookie is only as safe as its settings, written at the end of the Set-Cookie line.
| Flag | What it does | Why it helps |
|---|---|---|
HttpOnly | JavaScript cannot read it | A bad script on the page cannot copy your ID card |
Secure | Only sent over HTTPS | It is never sent as plain text on public Wi-Fi |
SameSite | Limits sending on cross-site requests | Another site cannot make your browser act for you |
SameSite has three settings. Strict sends the cookie only for requests that start on the cookie's own site. Lax also allows it when you click a link to that site from elsewhere. None sends it everywhere, and needs Secure.
A full example from MDN:
Set-Cookie: id=a3fWa; Expires=Thu, 21 Oct 2021 07:28:00 GMT; Secure; HttpOnly
OWASP's advice for session cookies is Secure, HttpOnly, and SameSite set to Strict (preferred) or Lax. It also warns against keeping session tokens in localStorage, because any script on the page can read that.
You can check this yourself.
Open any site you are logged into. Press F12, go to the Application tab (Storage in Firefox), and look under Cookies. Which ones have the HttpOnly box ticked?
Now open the Console and type:
document.cookie
Guess first: will the HttpOnly ones show up? They will not. That is MDN's rule in action: they only travel to the server, never to JavaScript. For the raw header, run curl -i https://example.com on any site that sets cookies and look for Set-Cookie.
All of this works while you are logged in. What about leaving?
With a server session, logout is easy: delete the row, and the ID card becomes a useless piece of paper. OWASP states logout must invalidate the session on the server. Clearing the cookie in the browser alone is not enough.
Signed tokens are harder. The server keeps no table, so there is nothing to delete. A stolen token keeps passing the signature check until it expires. (This follows from how they work. It is not a quote from the docs.)
The usual fixes are short-lived tokens or a blocklist of revoked ones. But a blocklist is a server-side table again, so you have basically rebuilt sessions.
OWASP also wants timeouts enforced on the server, never by a timer in the browser. Its example idle timeout is 15 to 30 minutes for low-risk apps.
HttpOnly helps limit.