> ## Documentation Index
> Fetch the complete documentation index at: https://notes.kodekloud.com/llms.txt
> Use this file to discover all available pages before exploring further.

# What Are Cookies

> Explains browser cookies, how session IDs work, cookie attributes, privacy concerns like third party tracking, and best practices for secure session and cookie management.

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.](https://mintcdn.com/kodekloud-c4ac6d9a/IK4ISE2GFis3psR1/images/DevOps-Interview-Questions-and-Answers-Scenario-Based-Prep/Networking/What-Are-Cookies/apple-server-to-laptop-abc123-iphone.jpg?fit=max\&auto=format\&n=IK4ISE2GFis3psR1\&q=85\&s=a3ca6be040511cefacd1b53b50b8e165)

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:

```http theme={null}
Set-Cookie: session_id=abc123; Max-Age=2592000; Secure; HttpOnly
```

Behind the scenes the server-side data might look like:

```text theme={null}
database
account   you@icloud.com
bag       iPhone Duo
sessionID abc123
```

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:

```http theme={null}
GET / HTTP/1.1
Host: apple.com
Cookie: session_id=abc123
```

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:

```text theme={null}
database
account      you@icloud.com
bag          iPhone Duo
session ID   abc123

cookies (stored by browser)
site         cookie               expires
apple.com    session_id=abc123    in 30 days
             Secure · HttpOnly
```

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

| Attribute | Purpose | Example |
| - | -: | - |
| `Max-Age` / `Expires` | How long the cookie should persist | `Max-Age=2592000` (30 days) |
| `Secure` | Send cookie only over HTTPS to prevent eavesdropping | `Secure` |
| `HttpOnly` | Prevent JavaScript access to mitigate some XSS attacks | `HttpOnly` |
| `SameSite` | Control cross-site sending behavior (`Strict`, `Lax`, `None`) | `SameSite=Strict` |

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.](https://mintcdn.com/kodekloud-c4ac6d9a/IK4ISE2GFis3psR1/images/DevOps-Interview-Questions-and-Answers-Scenario-Based-Prep/Networking/What-Are-Cookies/third-party-cookies-ads-789-travel.jpg?fit=max\&auto=format\&n=IK4ISE2GFis3psR1\&q=85\&s=d67068ff4e8164bf5aed57f820d6bd99)

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.](https://mintcdn.com/kodekloud-c4ac6d9a/IK4ISE2GFis3psR1/images/DevOps-Interview-Questions-and-Answers-Scenario-Based-Prep/Networking/What-Are-Cookies/laptop-cookie-settings-eu-flag.jpg?fit=max\&auto=format\&n=IK4ISE2GFis3psR1\&q=85\&s=aabebbc6fb0a60d95a8dd1e1d582b5d9)

Links and references

* [GDPR overview](https://gdpr.eu/)
* MDN Web Docs — Cookies: [https://developer.mozilla.org/en-US/docs/Web/HTTP/Cookies](https://developer.mozilla.org/en-US/docs/Web/HTTP/Cookies)
* OWASP — Session Management Cheat Sheet: [https://cheatsheetseries.owasp.org/cheatsheets/Session\_Management\_Cheat\_Sheet.html](https://cheatsheetseries.owasp.org/cheatsheets/Session_Management_Cheat_Sheet.html)

> [card] [Watch Video](https://learn.kodekloud.com/user/courses/devops-interview-prep/module/c1eb3967-23d3-4a34-b23d-14a892f95e1d/lesson/0a61564b-2838-4acf-9af2-4cd9596f8079)


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.