Cookie
In one line
A cookie is a small piece of data that a server sends to the browser, which is included in subsequent requests to the same server.
In simple words
Cookies are small text files that websites store in the browser. The server sends a cookie via the Set-Cookie header, and the browser automatically includes it in every subsequent request to that domain. This makes cookies ideal for session management and authentication.
Cookies have important attributes: HttpOnly (prevents JavaScript access, protecting against XSS), Secure (only sent over HTTPS), SameSite (controls cross-site sending, protecting against CSRF), and max-age/expires (expiration time).
Unlike localStorage, cookies are sent with every HTTP request to the matching domain, which adds overhead. They are limited to about 4KB each. Use cookies for authentication tokens and session IDs; use localStorage for client-only data.
Example
Set-Cookie: session_id=abc123; HttpOnly; Secure; SameSite=Strict; Max-Age=3600
// Browser sends this automatically:
Cookie: session_id=abc123A server sets a secure, HttpOnly, SameSite cookie. The browser automatically includes it in future requests.
Related terms
Related Resources
Related Lessons
- Cookies and Sessions
Learn cookies and sessions.
Decode error messages
Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.