Course contentsShow
← Security
Lesson 722 of 3,05215. Web Security - Authentication and SessionsFree lesson

Cookie Fundamentals and Attributes

Understanding cookie structure, Domain, Path, Expires/Max-Age attributes, and how browsers store and transmit cookies.

Cookie Fundamentals and Attributes

What you'll learn: How cookies are structured and how their attributes control where, when, and how browsers store and send them.

What is a Cookie?

A cookie is a small piece of data that a web server sends to your browser, which the browser stores and sends back with future requests to the same server. Think of it like a claim ticket at a coat check: the server gives you a token, you keep it, and you present it each time you return so the server remembers who you are.

Cookies are the primary mechanism for maintaining sessions in web applications. When you log in, the server typically creates a session and sends a cookie containing your session identifier.

Cookie Structure

A cookie consists of a name=value pair plus optional attributes that control its behavior:

Set-Cookie: sessionId=abc123; Domain=example.com; Path=/; Expires=Wed, 09 Jun 2025 10:18:14 GMT; Secure; HttpOnly

Key Attributes

Domain: Controls which domains can receive the cookie. If set to .example.com, the cookie is sent to example.com and all its subdomains like api.example.com. Without this attribute, the cookie only goes to the exact domain that set it.

Path: Restricts the cookie to specific URL paths. Path=/account means the cookie is only sent for requests to /account and below, not to /products.

Expires/Max-Age: Determines cookie lifetime. Expires sets an absolute date/time, while Max-Age specifies seconds from now. Without either, the cookie is a "session cookie" that disappears when the browser closes.

How Browsers Handle Cookies

When a server sends Set-Cookie, the browser stores it. On subsequent requests matching the Domain and Path, the browser automatically includes the cookie in the Cookie header—no JavaScript needed.

Key Takeaway: Cookie attributes (Domain, Path, Expires/Max-Age) act as rules that tell browsers exactly where, when, and for how long to store and transmit cookies—critical for both functionality and security.