Back to Blog Web Security

You Closed the Browser and Came Back — How Did Amazon Know It Was You?

Jun 25, 2026 8 min read Srikanth Badavath

Reading…
 Cookies · HTTP · Browser Storage
Tiny files. Giant memory.
Every website you visit drops a small breadcrumb in your browser. That breadcrumb is a cookie — and it is the reason the web feels like it knows you.
~50 billion
cookies set every day

1 The Puzzle

You open Amazon. You type your email, your password. You add three things to your cart. You close the browser tab. You go to sleep.

The next morning you open a new browser window, navigate to amazon.com, and — without typing a single character — you are still logged in. Your cart still has those three items. Your dark mode preference is still set.

HTTP, the protocol your browser uses to talk to servers, is completely stateless. Every single request is supposed to be a fresh conversation with no memory of what happened before. So how, exactly, does Amazon remember you across an entire browser restart?

The answer is one of the most quietly powerful mechanisms in all of web technology: the HTTP cookie.

2 What Is a Cookie?

A cookie is a small piece of text — typically fewer than 4 KB — that a web server tells your browser to store on your computer. The next time you visit that server, your browser automatically sends the cookie back. That is literally the entire mechanism. The server sets it; the browser stores it; the browser returns it on every subsequent request.

The magic is in the combination of three HTTP messages. Here is the exact exchange that happens when you log into a website for the first time:

The 3-Step Cookie Exchange
Your Browser
Web Server
1
Browser sends credentials to server
POST /login HTTP/1.1 Host: amazon.com Content-Type: application/x-www-form-urlencoded email=you@example.com&password=••••••••
2
Server validates login & sets a cookie
HTTP/1.1 200 OK Set-Cookie: session_id=a3f9k2mXbc21; HttpOnly; Secure; SameSite=Strict Expires=Thu, 25 Jun 2027 00:00:00 GMT Content-Type: text/html
3
Next visit — browser automatically sends cookie back
GET /dashboard HTTP/1.1 Host: amazon.com Cookie: session_id=a3f9k2mXbc21 // Server looks up this ID → "That's user #49284 → show their dashboard"
Key insight: The server never stores "User 49284 is currently logged in." Instead, it stores a mapping: session ID a3f9k2mXbc21 belongs to user 49284. When the browser sends that ID, the server does the lookup. The cookie itself is just a ticket — the data lives on the server.

3 Browser Cookie Storage (DevTools Simulation)

You can inspect every cookie your browser has stored for any website by opening Chrome DevTools, clicking the Application tab, and expanding Cookies. Here is a simulation of what that looks like for three popular sites:

Laptop showing a browser with developer tools open
Open Chrome DevTools (F12) → Application tab → Cookies. Every cookie your browser holds for that domain is listed there — name, value, domain, expiry, size.
Chrome DevTools — Application > Storage > Cookies
Application
Network
Console
Load cookies for:
Name Value Domain Expires Size
Click a site button above to inspect its cookies

4 Cookies Do More Than Login

Most people associate cookies with staying logged in — but that is just one of three major jobs a cookie can do. Any persistent state that the server needs to remember about you, without requiring you to be authenticated, can be stored in a cookie.

Your Cart
Add items to an Amazon cart without being logged in. Close the browser. Reopen it days later — your cart is still there. A cookie stores your anonymous cart ID, which the server maps to a list of items.
Preferences
Dark mode, language selection, currency, accessibility settings — all stored in cookies. The server reads them on your first request and renders the page accordingly, before you even interact with it.
Recently Viewed
Product browsing history, video watch progress, article reading position — cookies let sites resume exactly where you left off, even without an account, by tracking a list of IDs tied to your browser.

5 Cookie Anatomy

When a server sends a Set-Cookie header, it is not just a name and a value — it is a semicolon-delimited string of attributes that define exactly how the cookie behaves. Click any segment below to learn what it does:

Set-Cookie header — click any segment
session_id=a3f9k2m…bc21
;
Expires=Thu, 25 Jun 2027
;
HttpOnly
;
Secure
;
SameSite=Strict
Click any colored segment above to see a detailed explanation of that attribute.

6 Third-Party Cookies & Cross-Site Tracking

First-party cookies are set by the website you are actually visiting — amazon.com setting a cookie when you are on amazon.com. But there is a second kind: third-party cookies, set by a domain that is not the site you are on. This is where tracking comes in.

Person browsing the web on a laptop — third-party cookies track your activity across sites
You browse site A, then site B — a third-party tracker sees both visits because your browser silently sends the same cookie to their server on every page.
How an ad network tracks you across three different websites
ShoeStore.com
You browse Nike sneakers. The page loads an ad tracker script from ads.tracker.com.
cookie SET
same tracker
DailyNews.com
You read an article. This site also loads ads from ads.tracker.com.
cookie READ
same tracker
Instagram
You scroll your feed. The tracker knows it is you — and serves a shoe ad.
shoe AD shown
First-Party Cookie Third-Party Cookie
Set by The site you are on (amazon.com on amazon.com) A different domain embedded on the page (ads.tracker.com on amazon.com)
Purpose Login, cart, preferences, session state Cross-site tracking, retargeting ads, analytics aggregation
Privacy impact Low — scoped to one site High — builds a profile across thousands of sites
Browser support Fully supported everywhere Being phased out — Safari/Firefox already block by default; Chrome follows

7 Session Cookies vs Persistent Cookies

Not all cookies are equal in lifespan. The two fundamental types differ in one critical way: whether they survive a browser restart.

Session Cookie
Lives in RAM — no Expires attribute
A session cookie has no expiry date in its header. The browser keeps it in memory for the duration of the browser session. The moment you close the browser — not just the tab, but the entire application — the cookie evaporates. Banking sites use session cookies deliberately: if you walk away and your browser closes, you are automatically logged out.
Status
Active while browser is open
Persistent Cookie
Survives restarts — has Expires attribute
A persistent cookie carries an explicit expiry date or Max-Age attribute. The browser writes it to disk. It survives tab closes, browser restarts, even system reboots — until the expiry date passes or you manually delete it. The Amazon "stay logged in" checkbox sets a persistent cookie that expires years from now.
Example cookie expires in
Loading...

8 Cookie Security Flags

A cookie without security attributes is a security vulnerability waiting to be exploited. Modern cookies carry three flags that each block a different attack vector. Toggle them off below to see what attack becomes possible:

HttpOnly
Blocks JavaScript from reading the cookie via document.cookie
PROTECTED
When ON: Even if an attacker injects malicious JavaScript into the page (XSS), the script cannot call document.cookie to steal your session token. The cookie is invisible to the JavaScript runtime entirely — it only travels via HTTP headers.

When OFF: Any JS on the page — including injected malicious code — can read document.cookie and exfiltrate your session ID to a remote server, instantly logging the attacker in as you.
Secure
Cookie is only sent over HTTPS connections, never plain HTTP
PROTECTED
When ON: The browser refuses to include the cookie in any HTTP request — only HTTPS. Even if a user accidentally visits http:// instead of https://, the session cookie stays home.

When OFF: On a public Wi-Fi network, an attacker running a man-in-the-middle sniffer can intercept unencrypted HTTP traffic and read the cookie value directly from the request headers. Session hijacking accomplished without breaking any encryption.
SameSite=Strict
Prevents the cookie from being sent in cross-site requests
PROTECTED
When ON: The browser will not include this cookie in requests that originate from a different site. A malicious page at evil.com cannot silently make your browser fire a state-changing request to bank.com carrying your session cookie.

When OFF: Cross-Site Request Forgery (CSRF) becomes trivially possible. A crafted link or invisible <img> tag on an attacker's page can trigger authenticated actions on your behalf — password changes, bank transfers, profile edits — using your stored session cookie.

9 The Full Cookie Lifecycle

Here is a complete picture of how a cookie is born, stored, and used across every request the browser makes:

flowchart TD
    A([User visits website]) --> B[Browser sends HTTP request]
    B --> C{Cookie header present?}
    C -->|No cookie| D[Server creates new session]
    D --> E[Server responds with Set-Cookie header]
    E --> F[Browser stores cookie on disk or in memory]
    F --> G[User navigates to another page]
    G --> H[Browser automatically includes Cookie header]
    H --> I{Server validates cookie}
    I -->|Valid session| J[Load user data and serve response]
    I -->|Expired or invalid| K[Reject and redirect to login]
    J --> G
    C -->|Cookie exists| I
    K --> A
      

10 Frequently Asked Questions

First-party cookies are not inherently dangerous — they are just a storage mechanism. The security concerns arise from misconfiguration (missing HttpOnly, Secure, or SameSite flags), from third-party tracking cookies that build surveillance profiles without your awareness, and from cookie theft via XSS attacks. A well-configured cookie from a reputable first-party site is no more dangerous than any other stored data on your computer. The broader privacy concern is not any single cookie, but the ecosystem of third-party trackers that aggregate data across thousands of sites to profile you without meaningful consent.

Yes — every major browser provides a straightforward way to delete cookies. In Chrome: Settings → Privacy and Security → Clear Browsing Data. In Firefox: Preferences → Privacy & Security → Clear Data. You can delete all cookies at once, or use DevTools (F12 → Application → Cookies) to delete specific cookies from specific sites. The consequence of deleting cookies is that you will be logged out of every site, your cart will be emptied, and your preferences will reset. Browser extensions like Cookie AutoDelete can automatically purge cookies from sites you close — a good middle ground for privacy without constant manual effort.

The EU's General Data Protection Regulation (GDPR), effective May 2018, and the earlier ePrivacy Directive (the "Cookie Law") require websites to obtain informed consent before setting non-essential cookies — specifically analytics and advertising cookies. Strictly necessary cookies (those required for the site to function, like login session cookies) are exempt. The law applies to any site accessible to EU residents, which is why the consent banners appear globally. The California Consumer Privacy Act (CCPA) imposes similar requirements in the US. The practical result is those ubiquitous cookie consent banners, which have been widely criticized for dark patterns that make rejection deliberately difficult.

Both cookies and localStorage store data in the browser, but they are fundamentally different in how they work. Cookies are automatically sent to the server with every HTTP request — that is their defining feature. localStorage data never leaves the browser; it is only accessible to JavaScript running on that page. Cookies are limited to about 4 KB; localStorage allows up to 5–10 MB. Cookies can be configured with security attributes like HttpOnly and Secure; localStorage has no equivalent flags, so it is always accessible to JavaScript (making it unsuitable for session tokens). Cookies were designed for server communication; localStorage was designed for client-side storage of application state that the server does not need.

First-party cookies are not going anywhere — they are fundamental to how the web maintains state. What is being phased out are third-party cookies: Safari (since ITP in 2017) and Firefox (since 2019) block them by default. Google Chrome announced the Privacy Sandbox initiative as a replacement, proposing APIs like the Topics API to enable interest-based advertising without individual cross-site tracking. After several delays, Chrome began restricting third-party cookies in 2024. The advertising industry is scrambling to adapt with alternatives like server-side tracking, first-party data strategies, and contextual advertising. The death of the third-party cookie does not end tracking — it just forces the industry to find more privacy-respecting (or more cleverly hidden) mechanisms.

Want to understand more about web security?

Cookies are one piece of the modern web security stack. The same browser that stores your session cookie also enforces TLS encryption, handles end-to-end encrypted messages, and processes one-time passwords. Keep reading: