A Cookie is a small text file sent by the server and stored in the browser, used to save user state information on the client side. Cookies mainly contain attributes such as name, value, domain, path, expiration time, secure flag, and HttpOnly flag.
How Cookies work:
- The server sends the Cookie to the browser through the Set-Cookie response header
- The browser stores the Cookie locally
- The browser automatically sends the Cookie to the server through the Cookie request header in subsequent requests
Main uses of Cookies:
- Session management: maintain user login status
- Personalization: remember user preferences
- Shopping cart functionality: save items in the shopping cart
- Tracking and analytics: collect user behavior data
Cookie limitations:
- Size limit: typically each Cookie is no more than 4KB
- Quantity limit: usually 50-100 Cookies per domain
- Security: can be stolen or tampered with, need to use security flags