Understanding Web Cookies
Tags: webdev, codenewbie, beginners, productivity
Published: 2021-12-19 12:30:42

_Web cookies often have a negative connotation. In reality, they are indispensable tools for websites and sometimes save you time. In this article, we will look at how cookies work, the different types of cookies, what benefits they can bring to your website or the regulations concerning them... In a next article we will see how to implement them in our web page code._

---

### Received ideas

Since the creation of cookies, many rumors have developed:
- Cookies are like viruses, they infect users' hard drives.
- Cookies are used to send spam.
- Cookies are used only for advertising. 

We will see in this article that all these rumors are false.


![Cookie hide](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yk5xpfo9ced5u7l0t9ie.png)

---

### How it works ?
A web cookie is defined by the HTTP communication protocol as a small text sent by an HTTP server to an HTTP client, which the latter will send back the next time it connects to servers sharing the same domain name. It consists of a text containing an arbitrary sequence of key-value pairs. It is stored on the client's computer for a specific period of time.

For example, if you log in to a website and don't want to log in the next time, sometimes you can check the box "keep my session active" or "remember me". In this case, one or more cookies are created and stored on your computer to automatically log you in the next time you access that website. The cookies can be the following:
- nickname : mypseudo
- password : azerty

(In reality the password cookies are often encrypted, for security)

---

### A bit of history
Now we can ask ourselves how long have cookies been around ? 

The term cookie is a derivation of magic cookie. In programming, a magic cookie is a packet of data that a program receives and sends back unchanged. In 1994, Lou Montulli, an American computer scientist, had the idea of using cookies for client-server exchanges on the Web. At the time, he was an employee of Netscape Communications, a company developing Web browsers.

![Netscape Browser logo](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1pk6hze5urugnrxx8cmz.jpg)

The first use of cookies was to determine whether visitors to the Netscape website had visited the site before. 

After being implemented in the Netscape 0.9 beta Web browser in 1994, cookies were integrated into Internet Explorer 2, released in October 1995. 

Cookies were accepted by default in browser settings, and users were not informed of their presence. The general public only learned of their existence after the Financial Times published an article on February 12, 1996. That same year, cookies received a lot of media attention because of possible privacy intrusions. 

From the end of 2014, we see a banner about cookies on many sites. Depending on the country and continent, the regulations are not the same.



![Cookie Banner exemple](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6wxnjuy5d4298fb8dggy.jpg)

---

### Why they have a bad reputation ?
So why do they have such a bad reputation ? 

On the one hand, cookies are stored on the customer's computer and therefore take up storage space. Even if it is often tiny, the accumulation of cookies can end up weighing a real weight in the storage of the device. 

On the other hand, as we have seen before, they can be an intrusion of privacy. Indeed, other types of cookies, called third-party cookies, can track users through the different sites they visit. For example, some advertising companies can track users across all pages where they have placed advertising images or a spy pixel. The knowledge of the pages visited by the user allows the advertising companies to target the user's advertising preferences. 

The ability to build a user profile is considered by some to be an invasion of privacy, especially when tracking is done through different domains using third-party cookies. For this reason, some countries have cookie legislation.

---

### To be in order
To be sure to comply with regulations, it is best to set up a cookie banner warning users. Ideally, the user can either accept all cookies or manage their preferences.

To save time when developing websites, many developers use cookie consent libraries. This way, their site follows the legislation, without requiring a large development effort.

For my part, I use [this library](https://github.com/clement-gaudiniere/librairie-cookie-consent), developed by myself 😆. It is very easy to use, and offers a clean and customizable banner for your website. To understand how it works, just read the github documentation. I will make an article detailing it later.

{% github clement-gaudiniere/librairie-cookie-consent %}

---

I hope you learned something about cookies, feel free to ask me any questions you may have. 👍

---

You want to support me ?

[![Buymeacoffee](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4b9u45k83m0gjbk2vo8m.png)](https://www.buymeacoffee.com/clemgaudiniere)

OR

 
[![Patreon](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/e784und5tziinkkhg0im.png)](https://www.patreon.com/clementGaudiniere)