What are HTTP cookies?
Useful first: HTTP · optional, not a requirement
Choose your depth
Stay with the essentials, or look closer.
YOUR GOALUnderstand how a cookie helps a website remember something between requests.
A small note for the next visit.
Remember a choice
Imagine choosing a compact layout on a reading website. The site can ask your browser to save a cookie such as layout=compact. On a later matching request, the browser can return that value so the server can use your choice.
A cookie is data, not a program that runs by itself.
[1]The same mechanism has different uses
Cookies can support preferences, sign-in sessions, and tracking. Their purpose depends on how a site uses the values. Calling something a cookie does not tell you whether it is necessary or privacy-friendly.
Browsers can block or delete cookies, so a website cannot assume its note will always come back.
[1] [3]Cookies or cache?
A browser cache can keep copies of responses, such as images, to reuse later. Cookies instead carry small remembered values, such as a layout choice or a sign-in identifier, with matching requests.
Deleting cookies can reset preferences or make a site ask you to sign in again. Clearing only cached images and files is a different action: it removes those saved copies. Check which data categories your browser will remove.
[1] [7] [8] [9]A cookie carries a small remembered value into later eligible requests.
Every cookie is a tracking program.
A cookie is stored data; preferences, sessions, and tracking are different uses of it.
Show the explanation
Choose the layout associated with that preference. The value can inform the site's response. Cookies do not automatically execute code or grant access to all other websites' data.
YOUR GOALDistinguish response instructions, returned values, and the scope that selects a cookie.
Follow the value across two requests.
Two headers, opposite directions
A response from https://read.example might contain:
Set-Cookie: layout=compact; Path=/; Max-Age=3600; Secure; SameSite=LaxIf accepted, a later same-site HTTPS request to that host may include:
Cookie: layout=compactSet-Cookie tells the browser what to store. Cookie carries selected name-value pairs back. The request does not repeat the expiry or security attributes.
Where the note can travel
With no Domain attribute, this cookie belongs only to read.example; it is not also sent to images.read.example. Setting an allowed Domain expands host coverage to that domain and its subdomains.
Path=/ covers all paths on the eligible host. A cookie scoped to /notes matches /notes/42, but not /notebook. Other cookie rules must still pass. Path filtering is not a security wall between applications.
How long can it last?
Max-Age=3600 requests a one-hour lifetime, not guaranteed retention. A session cookie omits both Max-Age and Expires. Browser session restoration can preserve it across restarts, so closing a window is not reliable logout.
A session identifier is not the whole session
In a server-managed session, the cookie holds an opaque identifier: a value meaningful to the server rather than a readable account record. The server looks it up to find the associated session and permissions.
Removing that browser cookie and invalidating the server's session are different actions. A proper logout must also make the old server session unusable.
[6]Storage instructions go in Set-Cookie; eligible values return in Cookie, with server state managed separately.
Deleting a cookie necessarily deletes the matching server session.
Deleting browser storage does not itself invalidate the server-side record.
Show the explanation
https://read.example/notes/42 Only the original host and the matching /notes path qualify here; expiry, security settings, and browser policy must also allow sending.
YOUR GOALEvaluate cookie protections without mistaking any one attribute for complete session security.
Separate transport, script access, and request context.
Different controls protect different boundaries
For an ordinary remote website, Secure requires HTTPS when sending the cookie; it does not encrypt the stored value. HttpOnly prevents page JavaScript from reading it through cookie APIs, but eligible JavaScript-initiated requests can still carry it.
SameSite limits cross-site sending. Explicit Lax permits some top-level navigations using safe methods, such as following a GET link. None permits cross-site use and requires Secure; browser privacy restrictions can still block it.
Why HttpOnly is not proof of user intent
Suppose an application accepts a change-of-address request merely because a session cookie accompanies it. An attacker may try to make the visitor's browser send that request without permission. This is cross-site request forgery, or CSRF; the attacker need not read the cookie first.
Use the framework's CSRF defenses, such as server-validated tokens for state-changing requests, and suitable request-origin checks. SameSite is useful protection, but its presence alone does not establish that every action was intended.
[5]Recognize the limits of a token
A session cookie is a credential when possession lets someone act as the signed-in user. The server must enforce session expiry and invalidate the session at logout; a browser-side lifetime is not a substitute.
For a hypothetical notes app, protecting the session token and checking permission to edit each note are separate requirements. Receiving a valid session does not automatically authorize every operation.
[6]Cookie attributes reduce specific risks; the server still owns authorization and session lifetime.
HttpOnly means JavaScript cannot cause an authenticated request.
It restricts cookie access, but the browser can attach the cookie to eligible requests initiated by scripts.
Show the explanation
The browser can attach it even though page JavaScript cannot read its value. Reading a cookie through JavaScript and the browser attaching it to a permitted request are different operations.
Keep asking. Check the details.
Source-checked using AI (OpenAI Codex) on . The checks covered sources, examples, and quizzes for this article version.
No human review is claimed. AI review can miss errors; check the original sources below.
- MDN — Using HTTP cookies
- RFC 6265 — Cookie scope, lifetime, and returned attributes
- MDN — Cookie request header
- MDN — Set-Cookie security and SameSite attributes
- OWASP — Cross-Site Request Forgery Prevention
- OWASP — Session Management
- MDN — HTTP caching guide
- Chrome Help — What deleting cookies can change
- Chrome Help — Separate browsing-data categories