What Are Cookies?
HTTP is stateless — each request is independent, and servers don't automatically remember previous interactions. But websites clearly do remember things about you: your login status, shopping cart contents, and preferences. They accomplish this using cookies.
A cookie is a small piece of data that a website stores in your browser. On subsequent visits, your browser sends that cookie back to the website, allowing the server to recognize you and recall information from your previous interactions.
Think of cookies like a claim ticket at a coat check. The attendant gives you a ticket (the cookie), and when you return with that ticket, they can retrieve your coat (your stored information). Without the ticket, they wouldn't know which coat is yours.
How Cookies Work
When a server wants to set a cookie, it includes a special instruction in its response headers. Your browser stores this cookie and automatically includes it in future requests to that same website.
Cookies contain simple text data — often just an identifier that the server uses to look up your information in its database. A cookie might contain something like session_id=abc123xyz, which tells the server which user record to retrieve.
Cookies have several properties:
- Name and value: The actual data stored
- Expiration: When the cookie should be deleted
- Domain: Which website can access the cookie
- Path: Which pages on the site can access it
- Security flags: Whether it requires HTTPS
Common Uses
Authentication: When you log in, the server creates a session and gives you a cookie containing your session ID. This cookie proves you're logged in on subsequent requests.
Preferences: Sites remember your language, theme, or other settings using cookies.
Shopping carts: E-commerce sites track what you've added to your cart, even before you create an account.
Analytics: Cookies help websites understand how visitors use their site by tracking page views and interactions.
Privacy Considerations
Because cookies can track your activity, they raise privacy concerns. Third-party cookies — set by domains other than the one you're visiting — enable cross-site tracking for advertising. Modern browsers increasingly block these, and regulations like GDPR require websites to obtain consent before setting non-essential cookies.
Understanding cookies helps you make informed decisions about your privacy while appreciating how websites provide personalized experiences.