HTTP forgets; cookies remember
HTTP is stateless: each request stands alone, and the server does not remember the one before. That is a problem for anything with a login. You do not want to type your password on every click.
Cookies solve it. A server sends a Set-Cookie header in a response; the browser stores the cookie and sends it back with every later request to that site.
Sessions
When you log in, the server typically:
- checks your password (and second factor),
- creates a session on its side, with a long random session ID,
- sends that ID to your browser in a cookie.
From then on, the cookie is your login. Any request carrying that session ID is treated as you, until the session ends.
That has a big consequence: steal the cookie, and you skip the login entirely, including two-factor authentication. This is called session hijacking. Modern phishing kits often act as a go-between to the real login page: the victim logs in for real, with two-factor authentication, and the kit captures the session cookie that comes back.
Cookie flags
The Set-Cookie header can carry flags that limit how a cookie may be used:
| Flag | What it does |
|---|---|
Secure |
Only sent over HTTPS, never over plain HTTP. |
HttpOnly |
JavaScript on the page cannot read it. A malicious script cannot steal it directly. |
SameSite |
Limits sending the cookie on requests that come from other sites (Strict, Lax, or None). |
Expires / Max-Age |
When it disappears. Without one, it is deleted when the browser closes. |
A good session cookie has Secure, HttpOnly, and a sensible SameSite value.
Hands-on: see cookies being set
curl -I shows the Set-Cookie headers a site sends, before any login:
curl -sI https://www.google.com | grep -i set-cookiecurl -sI https://www.google.com | grep -i set-cookiecurl.exe -sI https://www.google.com | Select-String -Pattern "set-cookie"For each line, look for the flags at the end: Secure, HttpOnly, SameSite=..., and an expires= date. The exact cookies change over time; what matters is reading the flags.
Hands-on: cookies in your own browser
Open a site you are logged into, open DevTools (F12), and find the cookie list:
- Chrome and Edge: Application tab, then Cookies in the left column.
- Firefox: Storage tab, then Cookies.
- Safari: Storage tab, then Cookies (turn on the Develop menu in Safari’s settings first).
Look at the columns for HttpOnly, Secure, and SameSite. Then go to the Console tab and type:
document.cookie
You only see cookies without HttpOnly. The HttpOnly ones, usually including the important session cookie, are hidden from JavaScript. That is the flag doing its job.
Logging out
Pressing Log out should end the session on the server, so the old cookie stops working even if someone copied it. Closing the tab usually does not. On shared or public computers, always log out properly.
What to notice
- HTTP forgets every request; cookies carry a session ID that makes the server remember you.
- A session cookie is a working login. Stealing it bypasses the password and two-factor authentication.
Secure,HttpOnly, andSameSitelimit how cookies can leak or be misused.document.cookiecannot seeHttpOnlycookies, which protects them from malicious scripts.