Skip to content
YouTurkey Tech
All web topics

Cookies and sessions

Intermediate

How a site remembers that you logged in: a session id in a cookie, sent back with every request, and what HttpOnly, Secure and SameSite protect you from.Builds on HTTP requests and responses

POST /login HTTP/1.1Host: example.comContent-Type: application/x-www-form-urlencoded username=sara&password=s3cret-pass HTTP/1.1 303 See OtherLocation: /accountSet-Cookie: sid=k8Fq2xW7pL0aZ3; Max-Age=86400; Path=/; HttpOnly; Secure; SameSite=Lax GET /account HTTP/1.1Host: example.comCookie: sid=k8Fq2xW7pL0aZ3 HTTP/1.1 200 OKContent-Type: text/html; charset=UTF-8
Messages

Empty

0/16Press Play, or step through with the arrows, to begin.
Step 0 of 16

Explanation

3 min read

On this page

What are cookies and sessions?

HTTP has no memory: every request stands on its own. To know that two requests come from the same person, a site needs something the browser sends back every time. That is a cookie: a small name=value pair that a server sets with a Set-Cookie header, and that the browser returns in a Cookie header with every later request to that site.

A session is what most sites put in it. Instead of storing "user: sara" in the cookie, where anyone could edit it, the server makes a long random , keeps "this id belongs to Sara" on its side, and puts only the id in the cookie.

  • Max-Age or Expires say how long the browser keeps the cookie. Without either, it is a session cookie, dropped when the browser decides the session is over.
  • Path and Domain say which requests get the cookie. Without Domain, only the exact host that set it does, not its subdomains.
  • Secure sends the cookie over HTTPS only, so it never crosses the network in plain text.
  • HttpOnly hides it from JavaScript (document.cookie), so a script injected into the page (XSS) cannot read the session id and send it elsewhere.
  • SameSite controls requests that come from other sites. Strict never sends the cookie with them, Lax sends it only when you follow a link (a top-level GET), and None always does, and then requires Secure. Chrome and Edge treat a cookie without SameSite as Lax, but not every browser does, so always set it.

What do people get wrong about cookies?

Mini example

Not every cookie is a login. Here the server remembers a display preference for a year, readable by the page's JavaScript because it is not secret:

POST /settings/theme HTTP/1.1Host: example.comContent-Type: application/x-www-form-urlencoded theme=dark HTTP/1.1 303 See OtherLocation: /settingsSet-Cookie: theme=dark; Max-Age=31536000; Path=/; Secure; SameSite=Lax

Quick check

A script injected into the page runs document.cookie. Which cookie can it not see?

Further reading

All steps as text
  1. Step 1:

    HTTP has no memory: each request stands alone, and the server doesn’t know it came from the same person as the last one. Sara opens the login form.

  2. Step 2:

    She submits the form. The browser sends a POST whose body is the form’s fields, encoded like a URL query. HTTPS keeps the password private on the way.

    Lines 1–5

  3. Step 3:

    The server checks the password against the hash it stored when Sara signed up. It never keeps the password itself.

  4. Step 4:

    It’s right, so the server starts a session: a long random id, saved with who it belongs to. Nobody can guess it.

  5. Step 5:

    The response sends Sara on to her account page and carries a Set-Cookie header with the session id. The browser keeps it in its cookie jar for example.com.

    Lines 7–9

  6. Step 6:

    Max-Age=86400 keeps the cookie for one day, and Path=/ sends it with every page of the site. Secure means only over HTTPS.

    Line 9

  7. Step 7:

    HttpOnly hides the cookie from the page’s JavaScript, so a script slipped into the page can’t steal it. SameSite=Lax matters in a moment.

    Line 9

  8. Step 8:

    The browser follows the redirect with a GET, and adds the cookie on its own in a Cookie header. Neither Sara nor the page’s code had to do anything.

    Lines 11–13

  9. Step 9:

    The server looks the id up in the session store and finds Sara. To the server, whoever sends this id is Sara.

  10. Step 10:

    So it answers with Sara’s account page. Every later request to example.com carries the cookie the same way, and she stays logged in.

    Lines 15–16

  11. Step 11:

    Later Sara opens another site. Its page hides a form that posts to example.com/account/delete and submits itself as soon as the page loads.

  12. Step 12:

    The form sends a POST to example.com from another site. Because of SameSite=Lax, the browser leaves the cookie out of this cross-site POST.

  13. Step 13:

    With no session id, the request is from nobody, so the server refuses it. Without SameSite, the cookie would have gone along and the account would be gone.

  14. Step 14:

    Back on example.com, Sara logs out. The server deletes the session from its store, so the id is worthless even if someone copied it.

  15. Step 15:

    The response also clears the cookie: a Set-Cookie with the same name and Max-Age=0 tells the browser to throw it away.

  16. Step 16:

    A session is a random id the server remembers, carried in a cookie the browser sends back by itself. HttpOnly, Secure and SameSite keep that id out of the wrong hands.