Skip to main content
What are cookies in your browser? Imagine you visit the Apple website, sign in, add the new iPhone 12 to your bag, and then close the browser. When you come back the next day, you’re still signed in and the iPhone 12 remains in your bag. How does the website remember you? When you sign in, Apple’s server verifies your credentials and creates a long random string called a session ID. The server stores that session ID in its database and associates it with your account.
A dark schematic showing an Apple server icon at the top connected by a line to a laptop below, with a highlighted session ID "abc123" between them. The laptop screen displays an Apple web page showing an iPhone.
Apple then returns an HTTP response that includes a Set-Cookie header. This header instructs the browser to store a small piece of text (the cookie). A cookie typically has a name and a value, for example session_id=abc123. The server can also add attributes that control how the browser stores and sends the cookie. Example Set-Cookie header:
Behind the scenes the server-side data might look like:
Your browser stores the cookie together with the site it belongs to (here, apple.com) and the cookie attributes (expiration, Secure, HttpOnly, etc.). When you visit Apple again, the browser finds matching cookies and attaches only the cookie name and value to outgoing requests in a Cookie header — the attributes themselves are not sent. Example request with cookie:
On the server, Apple looks up abc123 and returns the personalized page that shows your shopping bag and account state. In this model the session ID acts as your login token while it is valid. A simplified view of the server-side record and the stored browser cookie:
If someone else obtains a valid session ID, they can impersonate you — possession of the session ID is effectively possession of your logged-in session. For this reason, use of Secure (send only over HTTPS) and HttpOnly (not accessible from JavaScript) is critical. Cookie attributes reference Cookies are a simple but powerful mechanism for maintaining state in the stateless HTTP protocol — session cookies for login state and persistent cookies for preferences or remember-me functionality. Why websites ask for permission Cookies can also be used to track users across multiple sites. For example, an advertising network can set a cookie when its ad is loaded on one site, then later read the same cookie when its ad appears on another site. Because the advertising cookie originates from a different domain than the page you’re visiting, it’s called a third-party cookie. Advertising companies can correlate visits across domains and build browsing profiles. Many browsers now restrict third-party cookies by default, and regulations such as the EU’s GDPR require consent before using cookies for tracking. Essential cookies (those necessary to keep you logged in or remember a shopping cart) are usually considered exempt from consent requirements.
An infographic about third-party cookies showing an ad company (id 789) linked to sites like shop.example and travel.example. A laptop mockup displays a travel.example page with ads and a cookies table listing ads.example id=789.
Third-party cookies are set by domains other than the one shown in the browser’s address bar. Browsers and privacy regulations increasingly limit their use to protect user privacy.
Practical considerations and best practices
  • Use Secure to ensure cookies are only sent over HTTPS.
  • Use HttpOnly to avoid exposing session tokens to client-side scripts.
  • Consider SameSite to limit cross-site requests and reduce CSRF risks.
  • Keep session lifetimes reasonable and rotate session identifiers on sensitive actions (like password changes).
  • Treat cookies containing authentication tokens as highly sensitive — use server-side session invalidation and short-lived tokens where appropriate.
Today, these small text files are what keep you signed in and preserve site preferences. Because of privacy concerns, many sites present a cookie settings UI so you can enable essential cookies while opting out of tracking cookies.
A laptop screen showing a website cookie settings pop-up with "Essential" cookies toggled on and "Tracking" toggled off. An EU flag icon appears above the laptop.
Links and references

Watch Video