What is a cookie? Explained with a cloakroom ticket
A cookie is a small note a website asks your browser to keep and show again on later visits, so the site recognises you. Explained with a cloakroom ticket: hand in your coat, keep the ticket, show it to get your coat back.
- Author
Umair Mateen- Published
- Reading time
- 5 min
- Category
- Engineering
In one sentence: A cookie is a small piece of text that a website asks your browser to keep and then hand back on every later visit, so the site can recognise you, keep you logged in or remember what is in your basket.
The everyday picture
Picture the cloakroom at a theatre. You hand over your coat. The attendant does not try to remember your face. Instead, they give you a small ticket with a number on it, and hang your coat on the hook with the same number.
All evening, you keep the ticket in your pocket. Whenever you come back to the counter, you show it. The attendant reads the number and knows straight away which coat is yours.
A website works the same way. On its own, a website forgets you the moment it answers you. A cookie is the ticket it hands you so it can recognise you next time.
The website hands your browser a ticket (the cookie). Your browser keeps it and shows it on every later visit, so the website can find what it stored for you.
How it works, step by step
- The first visit (handing over your coat): your browser asks the website for a page. The rules browsers and websites use to talk, called HTTP, do not remember earlier visits. Every request arrives like a stranger at the counter.
- The Set-Cookie instruction (the attendant hands you a ticket): along with the page, the website sends a short note that says, in effect, “keep this”. It is usually a name and a value, such as a random reference number.
- The browser’s cookie store (your pocket): your browser files the cookie away, along with a few labels. These say which website it belongs to, which parts of the site it is for, and when it should be thrown away.
- The Cookie header (showing your ticket): on every later request to that website, your browser automatically hands the cookie back. You do not see this happen.
- The lookup (finding your coat): the website reads the reference number and finds what it stored about you, such as your login or your shopping basket. The cookie itself usually holds just the number; the coat stays on the website’s own rack.
Try it · Myth or fact
Cookies: myth or fact?
Decide for yourself first, then tap each statement to check it.
0 of 4 checked
Where the picture stops
- A cloakroom ticket only works at its own counter. A cookie is the same: your browser only hands it back to the website (or the part of the website) that set it. But a “third-party” cookie belongs to another company whose content appears on many sites, like an advertiser. Picture one cloakroom chain with counters in many buildings, recognising the same ticket in each. That is how visits get tracked across sites.
- A paper ticket lasts the evening. A cookie with no expiry date usually goes when you close the browser; these are called session cookies. Others carry a date or a lifetime in seconds and stay for days or months; these are persistent cookies. Browsers can also clear them early, and you can delete them yourself.
- Anyone who steals a ticket can collect the coat. That is why websites can label a cookie Secure (only sent over an encrypted connection) and HttpOnly (hidden from the page’s scripts). These labels protect the cookie’s secrecy, but the standard notes they do not make it tamper-proof.
Why it matters for your business
- Customers: cookies are what let people stay logged in and keep their basket as they move between pages. Without them, every click would feel like arriving for the first time.
- Risk: a login cookie is as good as a password while it lasts. Your developers should mark it Secure and HttpOnly, and use random reference numbers rather than real customer details.
- Rules: in the EU, cookies that are strictly needed for a service the visitor asked for, like a basket or a login, do not need consent. Cookies for analytics, market research or behavioural advertising do, and they must not be set before the visitor agrees. Withdrawing consent must be as easy as giving it. Other countries, including the UK, have their own rules.
Try it · What would you do?
Adding cookies to an EU shop: which approach fits the rules?
An online shop with EU customers uses a basket cookie and wants to add an analytics tool that also sets cookies. How should it handle consent?
Pick what you would do. You can change your mind.
An example
Imagine a small bakery that sells cakes online. When a customer adds a cake to the basket, the site hands their browser a cookie holding a random basket number. The browser shows that number on every page, so the basket follows them to checkout. Later, the bakery adds a visitor-statistics tool. Because that tool uses cookies for analytics, the bakery adds a clear banner with equal “Accept” and “Reject” buttons, and only switches the tool on after a customer accepts.
Frequently asked questions
Are cookies dangerous?
A cookie is plain text, not a program, so it cannot run on your computer. The concerns are privacy, when cookies track you across sites, and theft, when someone copies a login cookie to pose as you.
What happens if I delete my cookies?
Websites stop recognising you. You will usually be logged out, and things like baskets or saved settings may be lost, just as a lost ticket means queuing to explain which coat is yours.
What is the difference between first-party and third-party cookies?
A first-party cookie is set by the website you are visiting. A third-party cookie is set by another company whose content, such as an advert or a widget, is loaded on that page.
Why do websites ask me to accept cookies?
Privacy rules in places like the EU require consent before a site sets cookies that are not strictly needed, such as those for advertising or analytics. The banner is how the site asks.
Related explainers
- What is a server? Explained with a kitchen that never closes
- What are SSO and 2FA? Explained with a hotel key card and a PIN
- What is encryption? Explained with a sealed envelope
- Frontend vs backend, explained with a restaurant
The DevDrift builds web platforms with secure logins and clear consent handling: see how.
Sources
Could you explain this to a colleague now?
Mark it and posts that mention it will show a tick. Saved in this browser only.