Tech News, Tech & Coding · Free Online Tools

IT Question Answer
Tools
Back to What Are

What Are Cookies in Web Browsers and How Do They Work?

What AreWeb Development BasicsBy Muhammad Fareed
cookieswebprivacybrowsers

Short answer: cookies are small pieces of text that a website asks your browser to store and send back on later requests. They let sites remember you, for example to keep you logged in, save a cart or remember settings. They can also be used for tracking, which is why privacy rules govern them.

How cookies work

  1. You visit a site, and the server includes a Set-Cookie header in its response.
  2. Your browser stores the cookie with its name, value and rules.
  3. On later requests to the same site, the browser sends the cookie back in a Cookie header.
  4. The server uses it to recognize your session or settings.
Set-Cookie: session=abc123; Path=/; Secure; HttpOnly; SameSite=Lax; Max-Age=86400

Main types of cookies

  • Session cookies: deleted when you close the browser, often used for login sessions.
  • Persistent cookies: stay until an expiry date, used for "remember me" and preferences.
  • First-party cookies: set by the site you are visiting.
  • Third-party cookies: set by another domain, such as an ad network embedded in the page. Many browsers now block or limit them.

Important cookie attributes

AttributeWhat it does
SecureSends the cookie only over HTTPS
HttpOnlyHides the cookie from JavaScript, which reduces theft through cross-site scripting
SameSiteControls whether the cookie is sent on cross-site requests, which helps against CSRF attacks
Max-Age / ExpiresSets how long the cookie lasts
Domain and PathDefine where the cookie is sent

Privacy and consent

Cookies used only to make a site work, such as a login session, are usually treated differently from cookies used for analytics and advertising. Laws such as the EU GDPR and ePrivacy rules require clear information and, for many non-essential cookies, consent. That is why many sites show a cookie banner.

How to manage cookies

  • Open your browser settings and review site data to see and delete cookies.
  • Block third-party cookies, or use private browsing for sessions you do not want to keep.
  • Use browser extensions or built-in tracking protection if you want stronger control.
  • Know that deleting cookies will log you out of most sites.

Tips for developers

  • Set HttpOnly, Secure and a suitable SameSite value on session cookies.
  • Keep cookies small, because they are sent with every request to the site.
  • Never store passwords or personal data directly in a cookie value.
  • Regenerate the session identifier after login to prevent session fixation.

Frequently asked questions

Are cookies dangerous?

Cookies are not programs and cannot run code. The risk comes from stolen session cookies or from tracking, which is why secure attributes and privacy settings matter.

What is the difference between cookies and local storage?

Cookies are sent to the server with every matching request, while local storage stays in the browser and is read only by JavaScript on that site. Local storage has more space, and cookies suit data the server needs.

Do I have to accept every cookie?

No. You can usually reject non-essential cookies in the banner. Essential cookies that keep the site working may still be set.