Cookies are one of the oldest mechanisms of storing the data on the client-side, or browser. But why do they exist in the first place? What problem do they solve?
In this video, you will exactly know the reason along with the hands-on demo using the Chrome Developer Tools
00:00 Hello there. Today you will learn why cookies are used by the browsers. The browsers use a protocol known as HTTP, also known as hypertext transfer protocol. The HTTP protocol is a stateless protocol, which means the request does not contain any unique information about the requester. This way, given a request, the server has no way of knowing whether or not the request is coming from the same computer. The cookies help to solve this problem. The cookies solve this problem for the servers. So when the request comes to the server, it sets a cookie in the HTTP response header and sends it back to the client. The client, in the subsequent request, sends the cookie to the server. That's how server solves the problem of knowing who the sender is. Let's see that in action. I have opened up the Chrome browser and not loaded any URL yet.
00:53 I will open up the Chrome developer tools and click on the Network tab. Since we have not loaded any URL, there is no activity that we see here. Now, I will load the URL google.com and as the page loads you can see the activity in the network tab. Next I will click onto google.com row with 302 response status since it is the one which sent the page back to us. This opens up a panel on the right and you can look at the cookies tab as the last tab here. Once clicked, you can see that the only cookies available here are the response cookies, which means server sent the cookies back to us. The browser requested the page for the very first time and had no cookies to send to the server. We can look at the applications tab and confirm that the cookies are available in the cookie storage.