Cookies are data which are stored in small text files on the end users machine. These files have information about users who are returning to your website. We can store the following type of information in cookies:-
- Track how many times the user is coming to your site.
- Which part of the site he visits most ?. So depending on which section he visits more you can show him those section first when he comes to the site.
To display the cookie back we can just write the below code.
If we have multiple cookies set we will get the value in name and value pair with semicolon separated as shown in the below sample.
There are two kinds of cookies permanent cookie and temporary cookie.
Permanent cookies are those which do not have expiry date and they are stored forever. The only way to delete these kinds of cookies is by going the browser and clearing cookies.
The second types of cookies have expiry date attached to it. So as soon as the date is surpassed the browser deletes that cookie. Below is a simple code which shows how to create a cookie with an expiry date.
Below is a simple real time example of counter which tracks using cookies how many times the user has visited the site. In the below code we first read the cookie counter , increment the counter value and store that counter value back to the cookie.