Learn about Cookies

Tram Ho

Hi guys,

In this article, we will learn about HTTP cookies and how to use JavaScript to manage cookies effectively.

1. What are cookies?

An HTTP cookie is a piece of data that a server sends to a web browser. The web browser then stores the HTTP cookie on the user’s computer and sends it back to the same server in future requests.

HTTP cookies are also known as web cookies or browser cookies. And it is often called a cookie.

For example, the header of an HTTP response might look something like this:

Consider the above example, the HTTP method will return us with the variable username and the value admin. The server encrypts both the name and the value when sending the cookie to the web browser.

The web browser stores this information and sends it back to the server via the HTTP Cookie header for the next request as follows:

2. Why Cookies?

As we know HTTP request is just stateless , because when we send two HTTP requests sequentially to the server, there is no link between them. In other words, the server cannot tell if two requests are from the same web browser.

Therefore, Cookie is used to indicate whether two requests are coming from the same web browser .

In fact, cookies serve the following purposes:

  • Session Management – cookies allow you to manage any information the server needs to remember. such as login information, shopping cart, etc.
  • Personalization – cookies allow you to store user information, themes and settings specific to the user.
  • Tracking – cookies help to record and analyze user behaviors in advertising.

3. Cookie details

A cookie consisting of the following information is stored in the web browser as a key-value (except for the secure flag)

InformationExplain
NameThe unique name that identifies the cookie. Cookie names are not case sensitive. It means Username and username are the same cookies
ValueThe string value of the cookie. It must be URL encoded.
DomainDomain to confirm cookie is valid
Paththe path does not have the domain name to which the cookie will be sent to the server. For example, you can specify that cookies are only accessible from https://www.yourwebsite.com/dom . Thus at https://www.yourwebsite.com will not send cookie information.
ExpirationThe interval indicates when the web browser will delete the cookie. The expiration date is set to a date in GMT format: “Wdy, DD-Mon-YYYY HH:MM:SS GMT”. The expiration date allows cookies to be stored in the user’s web browser even after the user closes the web browser.
Secure flagIf specified, the web browser only sends the cookie to the server via an SSL connection (https, not http)

Names, values, domains, paths, deadlines, and security flags are separated by semicolons and spaces. Such as:

Note that the secure flag is the only part that is not a key-value pair.

So basically in this article I learned about what Cookies are and why I use Cookies, I will summarize the main points as follows:

  • A cookie is a piece of data that a server sends to a web browser. The web browser then stores the cookie in the user’s computer and sends the cookie back to the same server on subsequent requests.
  • The server uses cookies to determine if two consecutive requests are coming from the same web browser.
Share the news now

Source : Viblo