Homepage > Journal > Cookies: How do they work and how to use them?
Journal

Cookies: How do they work and how to use them?

How you like that:

Nowadays, cookies are an indispensable part of using the Internet. Thanks to them, internet browsers can remember users' preferences and follow their actions on a website.

In this article, we’ll discuss what cookies are, how they work, and how to use them safely.

Key information

  • The main goal of cookies is to record information about a user and their preferences.
  • Cookies consist of attributes that, when appropriately defined, make them function correctly and securely.

We develop websites that boost sales

What are cookies, and how do they work?

Cookies are short text files that record data about user preferences. They’re stored in the browser on the user’s device and sent back to the server every time the user returns to the site.

Graphic illustrating what cookies look like in code

Thanks to cookies, the browser can remember information such as the site's preferred language, login data, or products in a shopping cart. Cookies also help website owners analyze user behavior regarding visited pages, clicked links, and used functionalities. This can, for example, enable them to improve the quality of the user interface and enhance user experience.

The server sends cookies during the first user visit to the website, signaling to the browser that it should save relevant data. The saved cookies are stored and called upon each subsequent visit. Usually, cookies have a defined expiration time; when the time is up, they stop working and are automatically deleted.

What do cookies consist of?

When the browser receives a cookie, it gets it in the form of a header: Set-Cookie. This header contains such attributes as:

  • Name and Value define the data that the cookie contains; for example, it can be username=johndoe123.
  • Domain and Path determine that the cookie will be sent only in response to a particular domain or path.
  • Expires and Max-Age define the cookie’s expiration date. The Expires value determines a specific date, and the Max-Age describes the duration. If these values aren’t set, cookies will be deleted after the user ends the session.
  • Secure informs the browser that the cookie should be sent only through HTTPS (encrypted protocol).
  • HttpOnly makes the scripts on the client side (like JavaScript) unable to access the cookies by injecting malicious scripts.
  • SameSite helps define when cookies should be sent to the server. SameSite can be set to Strict, Lax, and None.

What cookies consist of

The Strict value means cookies will only be sent if the request originates from the same domain defined in the cookies. Lax signals to the browser that cookies won’t be sent in the case of cross-site subresource requests but will be sent for top-level navigation (when the user clicks on a link leading to the website). None means that cookies will answer every request, but only when they have a defined atribute Secure; otherwise, the cookie won’t function properly.

How to use cookies safely

As we’ve mentioned, ensuring that cookies have defined appropriate attributes contributes not only to their proper functioning but also to the security level.

That’s why cookies should:

  • Have a defined Secure attribute
  • Be sent only through encrypted protocols
  • Have appropriately defined SameSite attribute to prevent potential attacks
  • Define how long they will be stored
  • Shouldn’t contain any sensitive data

A screenshot of Google Console illustrating browser cookies

Additionally, website owners should do their best to regularly debug cookies through tools such as Google Developer Tools.

Summary

Cookies support communication between the browser and the website’s server, allowing the former to save user information, such as their preferences or behavior.

The Set-Cookie header consists of several attributes. These define what types of data will be stored in the cookie and for how long, and they also define which sites will have access to the data stored in the cookie.

The security of the cookies also depends on the correct definition of attributes and the frequency of debugging.

Frequently asked questions

How do browser cookies work?

Cookies are information sent by the server to the browser that contains data on users using the website. They’re called upon each subsequent user visit to the site until they’re deleted.

Cookies help users keep their preferences regarding the website, such as products left in a shopping cart or the site’s language. Website owners can use them to track user behavior and adapt the site's functionalities to their needs.

What attributes do cookies consist of?

The Set-Cookie header consists of the following attributes: Name, Value, Domain, Path, Expires, Max-Age, Secure, HttpOnly, and SameSite. These attributes determine the type of information stored in the cookies, define the expiration date, and determine which domain can access them.

What influences the security of cookies?

Appropriately defined attributes such as Secure, HttpOnly, and SameStie influence cookie security. Thanks to them, you can send cookies through encrypted protocols, protect them from malicious scripts, and define domains that will have access to them.

How you like that:
Journal / Redaktor
Author: Radek
UX Writer and researcher by education + experience. Collects The Story's knowledge and shares it on the Journal.

Are you interested in working with us? Take a look at our Portfolio