How do cookies get into a browser?
Any website can place a cookie in a visitor’s browser as soon as the visitor visits the website. At that point a cookie, or several cookies, are stored into the users browser. After the cookie is saved, the browser then sends all the data from the cookie with each new request to the webserver of that specific website (domain), making all the data stored in the cookie available to the website on every new page load request. The website can add or modify the data stored in the cookie or can delete the cookie with each new page response.
When the website wants to save a cookie he will send that information to your browser and your browser will accept it and store it as a textual file on your computer. Cookies without expiration date and time are called session cookies while those that contain an expiry date and time are called persistent cookies. Once the expiry time is reached the browser will remove the cookie automatically. But cookies can also have a very long expiry time, years into the future, so technically these will never expire and can stay in your browser for a very long period of time: basically forever unless the user removes them.
If you use more than one web browser then each one of them will have their own cookies and they can’t read each other’s cookies. So for example if you have cookies saved in Chrome - Firefox can’t access any of these cookies from Chrome and vice versa. The reason behind that is that the cookies are the technology on a browser level - not on your operating system level. Also, the data saved in a cookie by one website (the actual domain) can not be accessed by another website domain (domain).
Here are a couple of examples to illustrate how this works and a couple of use cases:
Example 1
Let’s say a user from the United Kingdom starts a web browser and loads a news website (for eg. DW) for the first time. The website is in German language by default and the user from the UK speaks only English language. So the user changes the website language to English. At that point a cookie is saved to the user’s browser saying that this user’s preferred language is English and the website loads the content in the language the user can understand for the first time. From this point on, the user continues to browse the news website, all the content served to the user is in the English language. A couple days later, the user comes back again to this website. The cookie still exists in his browser and the website serves him the content in English language by default.
Example 2
A user visits a Gmail website (or Facebook, Twitter or pretty much any other website) and the website prompts the user to log in. The user logs in by entering his credentials - and at that point in time a cookie is dropped (saved) in the user's browser. As the user continues to browse the website - it will know who this user is and will serve a content relevant for him: his emails or messages etc. If the user doesn’t log out, closes the browser and comes back tomorrow, based on the data stored in the cookie, the website will know who he is and won’t ask the user to login again.
It is also important to mention that cookies can store only numbers and textual data - making them pretty safe for your computer as they can’t contain viruses or trojans, but cookies can be used to track you across the internet and can violate your online privacy.