Cookie
Key points
- A cookie is a small piece of data that a server has the browser hold onto, automatically sent back on later requests.
- There are "first-party cookies" issued by the site itself and "third-party cookies" issued by other sites — the latter face tightening restrictions for privacy reasons.
- As restrictions tighten, alternatives like Web Storage and token-based authentication are seeing wider adoption.
Cookie overview
A cookie is a small piece of text-format data that the browser stores at the server's instruction. Once stored, the browser automatically sends it back to the server on subsequent requests to the same site.
stores it
from then on
and checks it
This "issue → store → auto-send" round trip is the foundation for a wide range of uses, from staying logged in to remembering what's in a shopping cart.
Teacher Pochi's hintThis is a lot like a shop's stamp card. At checkout, you get a stamp added (issued), and you take it home in your wallet (stored). The next time you visit and show the card (sent), the shop instantly knows "this is a regular customer."
First-party cookies vs. third-party cookies
There are "first-party cookies," issued by the site you're currently viewing, and "third-party cookies," issued by a different domain embedded in that page — such as an ad or analytics tag.
Because third-party cookies can track which sites a user visits across the web, many browsers have been tightening restrictions on them in recent years for privacy reasons — we'll look at that in more detail shortly.
Teacher Pochi's hintA first-party cookie is like "that particular shop's own stamp card." A third-party cookie is more like "a shared points card accepted at many different shops" — with a shared card, the company running it can see every shop you've visited.
Cookie headers
Cookies are exchanged through two kinds of headers: the "Set-Cookie" header, attached to a server's response, and the "Cookie" header, attached to a browser's request.
Set-Cookie:
session_id=abc123→stores it
Cookie:
session_id=abc123and checks it
A cookie stored in one browser is automatically attached, as this "Cookie" header, to every request that browser sends to the same site. This is what makes the "staying logged in while moving between pages" experience possible.
Cookie attributes
The Set-Cookie header can carry additional "attributes" that control its expiration and safety. The most common ones are:
A "keep me logged in" checkbox is often implemented through this expiration difference, and the "SameSite" attribute we'll cover next plays a big role in the tightening restrictions on third-party cookies.
Teacher Pochi's hintThink of Secure as "only transporting it in a locked car," and HttpOnly as "making sure only the driver, and no one else, can see what's inside the package." Both are safeguards against a valuable cookie being intercepted along the way.
Common uses for cookies
Cookies are used for a wide range of purposes, including:
Cookie restrictions keep tightening
Third-party cookies in particular have drawn scrutiny for enabling cross-site behavioral tracking, and browsers and regulations alike have been tightening restrictions on them year after year.
In response, websites have been shifting toward mechanisms that don't rely so heavily on cookies alone.
Teacher Pochi's hintIt's like flyers you used to be able to hand out freely, but now need permission for in advance. As privacy awareness has grown, the rules around this have gotten stricter and stricter.
Alternatives to cookies
Mechanisms that either replace or complement cookies have started to see use, including:
That said, each of these alternatives comes with its own constraints, so cookies aren't going away entirely — the right choice depends on the use case.
Summary
A cookie is issued, stored, and sent via the Set-Cookie/Cookie headers — a basic mechanism websites use to identify users. The first-party/third-party distinction, along with attributes like expiration, Secure, HttpOnly, and SameSite, let you control how it's used and how safe it is. At the same time, restrictions on third-party cookies keep tightening, and adoption of alternatives like Web Storage and token-based authentication is growing.
Related topics: