Significance of Cookies


Significance of Cookies

A cookie is a little piece of data passed back and forth between the server and the client.

A cookie is a string that includes information in the form of a name-value pair separated by semicolons.

It remembers the user's information and preserves its state across all web pages.

It's vital to keep in mind that HTTP is a stateless protocol.

There is no connection between two consecutive HTTP requests sent to the server.

To put it another way, the server has no way of knowing if the two requests are from the same web browser.

  • As a result, a cookie is utilized to determine if the two requests originated from the same browser.
  • Cookies are used for the following purposes in practice:
  • Session management - cookies let you control any information you want the server to keep track of.
    For instance, logins, shopping carts, and so on.
  • Personalization - cookies allow you to save individual user preferences, themes, and settings.
    Tracking - cookies allow advertisers to track and analyze user activity.

Example :

<html> <head> <script type = "text/javascript"> function createCookie() { if( document.myform.customer.value == "" ) { alert("please enter some value."); return; } cookievalue = escape(document.myform.customer.value) + ";"; document.cookie = "name=" + cookievalue; document.write ("Cookies : " + "name=" + cookievalue ); } </script> </head> <body> <form name = "myform" action = ""> <input type = "text" name = "customer"/> <input type = "button" value = "Set Cookie" onclick = "createCookie();"/> </form> </body> </html>

OUTPUT: