How Browser Cookies Work
Cookies have a reputation as something sinister, mostly because of the banners that ask you about them on almost every site. In reality a cookie is a very small piece of text that a website asks your browser to remember. What matters is what that text is used for.
This guide explains how cookies are created, how they travel between your browser and a server, and what you can do to keep them under control.
Why Cookies Exist
The web's basic protocol, HTTP, is stateless. Each request is independent, and the server does not automatically remember the previous one. Without some memory, a shop could not keep items in your cart as you move from page to page, and you would have to log in again on every click.
Cookies solve this. The server hands the browser a small label, and the browser sends that label back with every later request to the same site. The server reads it and recognizes the returning visitor.
How a Cookie Is Set
A cookie begins in an HTTP response. The server includes a header such as this:
Set-Cookie: session_id=abc123; Path=/; Secure; HttpOnly; SameSite=Lax
The browser stores the name and value along with the attributes. On later requests to a matching site, it adds a header like this:
Cookie: session_id=abc123
JavaScript on a page can also create cookies through document.cookie, unless the cookie is marked as off limits to scripts.
What the Attributes Mean
The settings after the value control when and how the cookie is sent.
- Domain and Path decide which addresses receive the cookie. A path of
/means the whole site. - Expires or Max-Age sets how long the cookie lives. Without either, it is a session cookie and disappears when the browser closes.
- Secure means the cookie is sent only over HTTPS.
- HttpOnly hides the cookie from JavaScript, which protects login tokens from certain script attacks.
- SameSite limits whether the cookie goes along with requests started from other sites. Values are
Strict,LaxandNone.
When you review cookies in your browser's developer tools, these are the fields you will see.
Session Cookies Versus Persistent Cookies
A session cookie has no expiry date and lives only until you close the browser. It is typically used for logins and shopping carts during a single visit.
A persistent cookie has an expiry date and survives restarts. It might remember your language, a "keep me signed in" choice or a dark mode setting.
First-Party and Third-Party Cookies
A first-party cookie comes from the site you are visiting. It usually supports features you actually asked for, such as staying logged in.
A third-party cookie comes from a different domain, typically because the page embeds content from it, like an advertising or analytics script. Because the same outside service can appear on many sites, its cookie could recognize you across all of them. This cross-site tracking is the main privacy worry.
Several major browsers now block third-party cookies by default or restrict them heavily, and sites increasingly rely on other methods. The details differ from browser to browser and change over time, so check your own browser's settings.
What Cookies Can and Cannot Do
A cookie is plain data, not a program. It cannot run code, read your files or install anything. It is also limited in size and number per site.
That said, cookies can be misused. A stolen login cookie may let someone act as you, which is why the Secure and HttpOnly flags matter. A tracking cookie can build a picture of the sites you visit. And a cookie that holds a readable name or email address is a poor design, since anyone with access to your device can see it.
Managing Your Cookies
You have more control than you might think.
- Open your browser's privacy or site settings and find the cookie section.
- Choose whether to allow, block or limit third-party cookies.
- Set the browser to clear cookies when it closes if you prefer a clean slate, keeping in mind that you will be logged out of sites.
- View stored cookies for a particular site and remove only those you do not want.
- Use a private window for sessions that should leave no trace on the device.
Clearing cookies can fix odd problems such as a login loop or a page stuck in an old state. It will also reset saved preferences, so expect to sign in again.
Cookies Versus Other Storage
Browsers offer other ways to keep data. Local storage and session storage hold larger amounts but are not automatically sent to the server with each request. Cookies are unique in that they travel with every matching request, which is both their strength and the reason to keep them small.
Frequently Asked Questions
Are cookies the same as viruses?
No. A cookie is just text that your browser stores. It cannot execute, spread or damage your device. The concern is privacy and account security, not malware.
Why do so many sites ask me to accept cookies?
Privacy laws in many regions require sites to tell visitors about non-essential cookies, such as those used for tracking or advertising, and often to obtain consent. Cookies that are strictly needed for the site to work, like a login session, are usually treated differently.
Will clearing cookies make my browser faster?
Not noticeably in most cases, since cookies are tiny. The main effects are logging you out of sites and resetting preferences. Clearing the cache or fixing a specific site problem is more likely to produce a visible change.