HTTP cookies, spelled out
HTTP Cookie
Set-Cookie, Domain, Path, SameSite
How the Set-Cookie and Cookie headers work, what each attribute does and where cookies fit among sessions, tokens and consent, explained in plain language.
Index
Cookie terms from A to Z
Pick a letter or open a card to read what a term means.
- CookieA cookie is a small named value that a browser stores at a server's request and sends back on matching later requests. It gives the state...
- Set-CookieSet-Cookie is the HTTP response header a server uses to create or update one cookie in the browser. It carries the cookie's name and valu...
- Cookie headerThe Cookie header is the HTTP request header in which a browser returns the cookies that match a request. It carries only names and value...
- Cookie jarThe cookie jar is the browser's store of cookies, keyed by name, domain and path. Each browser profile keeps its own, so a private window...
- Cookie attributeA cookie attribute is an option written after the first semicolon of a Set-Cookie header, such as Path, Max-Age, Secure or SameSite. Attr...
- DomainThe Domain attribute makes a cookie available to a named domain and all of its subdomains. If it is left out, the cookie is host-only and...
- PathThe Path attribute limits a cookie to requests whose URL path starts with the given value. Path=/admin covers /admin and /admin/users but...
- ExpiresThe Expires attribute sets an absolute expiry date and time, written as an HTTP date in GMT, such as Expires=Wed, 21 Oct 2026 07:28:00 GM...
- Max-AgeThe Max-Age attribute sets a cookie's lifetime as a number of seconds counted from the moment the browser receives it. Max-Age=3600 means...
Guides
Cookie guides
Guides on the headers, attributes, prefixes, SameSite, third-party cookies, debugging and privacy.
-
How the Set-Cookie and Cookie Headers WorkA plain walkthrough of how a server sets a cookie with Set-Cookie and how the browser sends it back with the Cookie header on later requests.
-
Domain, Path, Expires and Max-Age ExplainedLearn how the Domain, Path, Expires and Max-Age cookie attributes decide where a cookie is sent and how long it lives, with common mistakes to avoid.
- Secure, HttpOnly and the __Host- and __Secure- PrefixesWhat the Secure and HttpOnly flags protect against, and how the __Host- and __Secure- cookie name prefixes make browsers enforce stricter rules for you.
- SameSite Strict, Lax and None ExplainedUnderstand the SameSite cookie attribute, the difference between Strict, Lax and None, and why cross-site requests sometimes arrive without your cookie.
- First-Party, Third-Party and Partitioned CookiesHow first-party and third-party cookies differ, why browsers limit the latter, and what the Partitioned attribute (CHIPS) is meant to do in general terms.
- Sessions vs Tokens: Where Cookies FitCompare server-side sessions and signed tokens such as JWT, when to keep them in cookies, why storage choice matters, and how to log out safely.
What is an HTTP cookie?
An HTTP cookie is a small named value that a server asks the browser to store with the Set-Cookie response header. The browser sends it back in the Cookie request header on later requests that match its rules, which lets a stateless protocol remember a login, a basket or a preference. The request header carries only the name and value, never the attributes. RFC 6265 is the specification that defines this mechanism, and MDN's guide to HTTP cookies is a good plain-language companion.
What is the difference between a cookie and localStorage?
A cookie is sent to the server automatically with matching requests, while localStorage never leaves the browser unless your own code sends its contents. Cookies can carry flags such as HttpOnly and Secure, so scripts cannot read an HttpOnly cookie. LocalStorage is readable by any script on the page, which makes it a weaker home for session tokens. MDN notes that modern storage APIs are recommended for general client-side storage because cookies are sent with every request and can hurt performance.
How big can a cookie be?
RFC 6265 says browsers should support at least 4096 bytes per cookie, counting name, value and attributes, at least 50 cookies per domain and at least 3000 in total. These are minimums that servers should plan around, and real limits vary by browser, so test the browsers you support. MDN describes about 4 KB per cookie as the usual maximum. Keep cookies small, because they travel with every matching request, and store bulky data on the server.
How do I delete a cookie?
Send a Set-Cookie header with the same name, domain and path as the original, and an expiry in the past or a Max-Age of zero or less. MDN describes this as the way to remove a cookie immediately. If any of the three identifying parts differ, the browser treats it as a different cookie and the original stays. This mismatch is the most common reason a logout appears to work while the user remains logged in.
What is a session cookie?
A session cookie is a cookie with no Expires or Max-Age attribute, so the browser treats it as temporary and normally removes it when the session ends. The browser decides when that is, and MDN warns that session restore can keep session cookies alive indefinitely. A session cookie is not the same as a server-side session; it usually just carries an identifier. For login state, prefer a short lifetime plus server-side idle and absolute timeouts.
Why is my cookie not sent with a request?
The usual causes are a different host, a path that does not match, a Secure cookie on a plain HTTP request, an expired lifetime, a SameSite restriction or a blocked third-party context. Work through them in that order using the browser's developer tools: the network panel shows Set-Cookie on responses and, for each request, which cookies were sent or filtered out, often with a reason. A cross-origin fetch also omits cookies unless credentials are enabled.
What does HttpOnly do?
HttpOnly hides a cookie from page scripts, so document.cookie cannot read it, while the browser still sends it with requests. MDN notes it is still included on fetch and XMLHttpRequest calls. This limits cookie theft through cross-site scripting, because injected script cannot copy a session identifier. It does not stop injected script from acting as the user through requests that carry the cookie, so it reduces damage rather than preventing XSS. Preventing injection remains the real fix.
What does the Secure flag do?
The Secure flag tells the browser to send the cookie only over encrypted connections, which means HTTPS. MDN notes that localhost is an exception for development. Current browsers also generally refuse to let an insecure response set or overwrite a Secure cookie, which blocks a trick where a network attacker plants a cookie. It only protects you if the whole site uses HTTPS, so pair it with redirects from HTTP and, where appropriate, Strict-Transport-Security.