Sobes.tech
Junior — Middle

Can you explain what a cookie is and what they are used for in web applications?

sobes.tech AI

Answer from AI

Cookie is a small piece of data sent by a web server and stored locally by the browser. On subsequent requests to the same server, the browser automatically sends these cookies back.

The main purpose of cookies is to store information about the user's state between requests, as HTTP is a stateless protocol. For example, cookies are used for:

  • Authentication (so the user doesn't have to enter login and password on each page)
  • Storing user settings
  • Session tracking and analytics

Example of setting a cookie in an HTTP header:

Set-Cookie: sessionId=abc123; Path=/; HttpOnly

In the browser, cookies can be read and used to control the behavior of web applications.