The Hypertext Transfer Protocol (HTTP) is a stateless protocol, i.e., each request must contain all the required information to fulfill the request. The server does not maintain information from prior client requests. However, this is not how the HTTP that we know today works. If it did, then usernames and passwords would have to be shared every time an HTTP request was sent to a website that required authentication.
A cookie is a key-value pair. It is stored on users’ computers by websites to remember notable information. The cookie is shared with the website that set it every time the user visits that website. It is important to note that cookies may have an expiration date.
The illustration below shows how a cookie is set and shared:
Some of the most important uses of a cookie are:
Storing the contents of users’ shopping carts at online shops.
Remembering user preferences on a website to provide a personalized experience for each user.