Skip to content
derpx06.dev
WritingExplainersNotesAbout
works on my machineit's always DNSship itLGTMTODO: sleepoff by oneread the docsgit push --force-with-lease404: sleep not foundhave you tried turning it off and on againrubber duck says: explain itpremature optimisationworks on my machineit's always DNSship itLGTMTODO: sleepoff by oneread the docsgit push --force-with-lease404: sleep not foundhave you tried turning it off and on againrubber duck says: explain itpremature optimisation

Learning in public.

A student's notebook on system design, distributed systems and databases. Shared as I learn.

ReadWritingExplainersNotesAbout
ElsewhereGitHubLinkedInHugging FaceLeetCode
Say hellomanaskhairnar1511@gmail.com
derpx06
NORMALmain© 2026 Derpx06Pune, Indiatop ↑
Blog/Security

How do websites remember you're logged in?

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.

Derpx0621 Oct · 4 min read
Contents
Why does the web forget me?How does a cookie get set and used?What does the cookie hold?What stops someone stealing my cookie?Can I see my own cookies?Why is logout the hard part?What to learn nextSources
0% read · 4 min leftBack to top ↑
On this page0%
Contents
Why does the web forget me?How does a cookie get set and used?What does the cookie hold?What stops someone stealing my cookie?Can I see my own cookies?Why is logout the hard part?What to learn nextSources

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.

Why does the web forget me?

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.

No memoryEvery request is a stranger. Log in, then click Profile: who are you again?
→
With a cookieEvery request carries an ID card. The server looks at it and recognizes you.

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?

How does a cookie get set and used?

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.

Browser
Server

You send your username and password

Step 1 of 5
Example cookie: sid=a3fWa. The server never asks for your password again.

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.

What does the cookie hold?

The cookie can hold a random label, or it can hold the whole answer.

Server session

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.

Signed token (like JWT)

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?

What stops someone stealing my cookie?

A cookie is only as safe as its settings, written at the end of the Set-Cookie line.

FlagWhat it doesWhy it helps
HttpOnlyJavaScript cannot read itA bad script on the page cannot copy your ID card
SecureOnly sent over HTTPSIt is never sent as plain text on public Wi-Fi
SameSiteLimits sending on cross-site requestsAnother 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:

Http
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.

Secure: encrypted in transit
HttpOnly: hidden from scripts
SameSite: not sent for risky cross-site requests
Server-side expiry: ends the session even if the cookie lives
Each layer covers a different way to lose your session.

You can check this yourself.

Can I see my own cookies?

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:

Js
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?

Why is logout the hard part?

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.)

Token issued
Server trusts the signature
Token is stolen
Still valid until it expires
↺ repeat
No list to cross a name off.

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.

What to learn next

  • Session fixation: why you should issue a new ID right after login.
  • CSRF: how another site can trick your browser into using your cookie.
  • XSS: the attack HttpOnly helps limit.
  • OAuth and "Login with Google": a different layer, built on top of all this.
Key takeaways
  • HTTP forgets you on every request, so the browser keeps showing a cookie to remind the server who you are.
  • Staying logged in is just a stored ID card, and the key question is how safe that ID card is.
  • HttpOnly, Secure and SameSite each block a different way to steal or misuse the cookie.
  • Logout is hard because a server session can be deleted, but a self-contained token cannot be un-trusted until it expires.

Sources

  • MDN: Using HTTP cookies
  • RFC 6265: HTTP State Management Mechanism
  • OWASP Session Management Cheat Sheet
ShareXLinkedInEmail
Written by Derpx06A developer who builds AI search tools and the code around them. I explain what I got wrong first, so you can skip it. Spotted a mistake? Tell me — corrections get credited.
Security · 5 minWhy password hashing is slow on purposeFoundations · 3 minWhy is 0.1 + 0.2 not 0.3 in code?Foundations · 4 minHow does Git work inside? (Snapshots, not changes)
Start here
01Why is 0.1 + 0.2 not 0.3 in code?02How does Git work inside? (Snapshots, not changes)03How does your computer know the time? (And why it can be wrong)