How cookies are used in a website?
What are cookies?
Cookies are small files which are stored on a user’s computer. They are used to hold a modest amount of data specific to a particular client and website and can be accessed either by the web server or by the client computer
When cookies were invented, they were basically little documents containing information about you and your preferences. For instance, when you select your language in which you want to view your website, the website would save the information in a document called a cookie on your computer, and the next time when you visit the website, it would be able to read a cookie saved earlier. That way the website could remember your language and let you view the website in your preferred language without having to select the language again.
A cookie can contain any type of information such as the time when you visited the website, the items that you added into your shopping basket, all the links you clicked in website, etc.,
Who can access?
If a cookie is created in a particular website, and you visit another website later, the latter would not be able to read the contents from the first website, in other words only the same website that saves information to a cookie can access it.
Types of internet Cookies
Session Cookies: A session cookie only lasts for the duration of users using the website. A web browser normally deletes session cookies when it quits. A session cookie expires if the user does not access the website for a period of time chosen by the server(idle timeout). They only last for the duration of time we are on the site. If someone comes and uses our computer, they would not be able to see anything on the sites that use session cookies, because they need to enter the username and password again.
Persistent Cookies: A persistent cookie outlast user sessions. If a persistent cookie has its maximum age 1 year, then within a year, the initial value set in the cookie would be sent back to the server every time the user visits the server. This could be used to record a vital piece of information such as how the user initially came to the website. For this reason, persistent cookies are also called tracking cookies. These are kind of cookies are used on websites that need to know who we are but offer us the ability to “remember me” when you enter username and password.
Third-party cookies: Third party cookies are the cookies being set with the different domain than the one shown in the address bar. For example, if you were to visit Sendflowers.com, it may set the cookie for the address of SomeAdvertiser.com. Later, when you visit RebuidEngines.com it may set the cookie for SomeAdvertiser.com. Both of these cookies will be used by SomeAdvertiser.com to ascertain that you are a person who sends flowers and works on cars. These are used by advertising agencies who have clients that pay for displaying ads for products and services but don’t want to waste money displaying them to people who aren’t going to be a customer.
HTTP Cookies: When you send a request to the server, the server sends a reply in which it embeds the cookie which serves as an identifier to identify the user. So, next time when you visit the same website, the cookie lets the server know that you are visiting the website again.
Related Article: Javax.servlet.http.Cookie class in Java
This article is contributed by Brahmani Sai. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.