How do cookies work?

In short
Cookies are small pieces of text that a server sends to your browser, and the browser sends them back on later requests. This lets a website recognize the same browser across visits, for example to keep you signed in. Cookies can expire, be deleted, and be locked down with security settings.
What cookies are for
HTTP is the protocol browsers use to talk to web servers, and it has no built-in memory. Each request stands on its own. Cookies fill that gap. They let a server tell whether two requests came from the same browser, so it can send a personalized page or a generic one.
A common example is sign-in. You submit your login details. If they are correct, the server stores a session, which is a record of your signed-in state, and gives your browser a cookie that holds a session ID. The session ID is a random identifier that points to that record. Every later request to the site includes the cookie, so the server can check the ID and keep you signed in.
Cookies are mainly used for three things:
- Session management, such as sign-in status, shopping cart contents or game scores.
- Personalization, such as language or color theme.
- Tracking, which means recording and analyzing how people use a site.
How a cookie is created and sent
A server creates a cookie by sending a Set-Cookie header in its response. The header holds a name and a value, written as a pair. A single response can set several cookies, each in its own header.
When the browser makes a new request to the same site, it normally attaches the cookies it has stored for that domain in a Cookie header. The server reads that header and decides what to do. Because the browser sends cookies automatically, you do not have to do anything in your page code for this to happen.
You can also create cookies in the browser with JavaScript, using the document.cookie property:
document.cookie = "theme=dark";
console.log(document.cookie); // shows the cookies visible to this page
JavaScript cookies are visible only to scripts on the same site, so they cannot be read by other sites.
How long cookies last
A cookie can be permanent or temporary. A permanent cookie gets an expiration time, either with the Expires attribute, which is a date, or with Max-Age, which is a number of seconds. If both are set, Max-Age wins. Max-Age is also less likely to cause errors, because Expires depends on the clock of the user's computer.
A cookie without either attribute is a session cookie. The browser deletes it when the session ends. The exact meaning of a session depends on the browser, and some browsers restore sessions on restart, so a session cookie can last longer than you expect.
To delete a cookie, send it again with the same name and set Max-Age to 0. The browser then removes it straight away. Note that a site that signs users in should send a fresh session cookie each time a user signs in. This helps stop session fixation attacks, where someone else reuses a session they have set up in advance.
Keeping cookies secure
By default, anyone with access to the browser can read cookie values and change them. Because of this, you should not store sensitive data directly in a cookie. A safer pattern is to store an opaque identifier, which is a random value the server uses to look up the real data on its side.
Two attributes help protect cookies:
- Secure means the browser sends the cookie only over an encrypted HTTPS connection.
- HttpOnly means JavaScript cannot read the cookie through document.cookie. The cookie is still sent to the server. This helps reduce the damage of cross-site scripting (XSS) attacks, where an attacker injects a script into a page.
The Domain and Path attributes control where a cookie is sent. If you set a Domain, the cookie is available on that domain and its subdomains. If you leave it out, the cookie goes only to the server that set it. That makes leaving Domain out the stricter choice.
Cookies or modern storage?
In the early web, cookies were the only way to keep data in the browser. Today, the Web Storage API (localStorage and sessionStorage) and IndexedDB are usually better for storing data that the server does not need. They do not travel with every request, and they can hold much more data. Browsers also limit cookies, usually to a few hundred per domain and about 4KB each.
You can look at cookies in your browser's developer tools. In Firefox, use the Storage Inspector. In Chrome, use the Application panel.
Common mistakes
- Storing large amounts of data in cookies. They are sent with every request, which can slow down slow mobile connections.
- Forgetting HttpOnly on session cookies, which leaves them open to script access.
- Trusting cookie values. Users can change them, so the server should always check what it reads.
If you are new to web security in general, see our other reference pages to learn the basics before you build a sign-in system.
FAQ
Are cookies the same as local storage?
No. Cookies are sent to the server with every request, while local storage stays in the browser and is never sent automatically. Local storage can also hold more data than a single cookie.
Can a website read cookies from other websites?
Normally no. A browser sends each cookie only to the domain that set it, unless the Domain attribute says otherwise. This limit is a core part of how cookies stay private.
How do I delete a cookie?
Set the cookie again with the same name, path and domain, and set Max-Age to 0 or a negative number. The browser removes it right away.
Why should I set the HttpOnly attribute?
HttpOnly stops JavaScript from reading the cookie through document.cookie. This makes it harder for an attacker who injects a script to steal a session ID.
Sources
This page is based on the official documentation below. Read it for full detail.
Cookies — official documentation ↗