Presentation is loading. Please wait.

Presentation is loading. Please wait.

Cookies The HTTP protocol is stateless, that is, it does not maintain information on states after the session ends. But sometimes it is useful to remember.

Similar presentations


Presentation on theme: "Cookies The HTTP protocol is stateless, that is, it does not maintain information on states after the session ends. But sometimes it is useful to remember."— Presentation transcript:

1 Cookies The HTTP protocol is stateless, that is, it does not maintain information on states after the session ends. But sometimes it is useful to remember customers preferences, registration numbers etc… A cookie is a local file used to store information about the viewer that can be retrieved and used at a later time to welcome him to your site. The HTTP server sends the cookie to the browser when the browser connects for the first time and from then on, the browser returns a copy of the cookie to the server each time it connects. Browsers usually cannot store more than 300 cookies and storage is limited to 4Kbytes per cookie.

2 Cookies Ingredients Cookies are usually sent from a CGI program from the server to the browser through HTTP request and response headers. With JavaScript you can set cookies on the local browser, eliminating the need for the CGI program to handle them. FORMAT of the HTTP Set-Cookie header: Set-Cookie: name=value; [expires=date]; [path=path]; [domain=domainname]; [secure]; EXAMPLE: Set-Cookie: id=“Bob”; expires=Monday, 21-Oct-05 12:00:00GMT; domain=“bbb.com”; path=“/”; secure;

3 Cookie’s attributes name FORMAT: nameofcookie=value; expiration date FORMAT: ; expires=Weekday, DD-MON-YY HH:MM:SS GMT domain name FORMAT: ; domain=.domain_name ; domain=http://somedomain.com path FORMAT: ; path=pathname secure FORMAT: ; secure

4 Cookie Object Cookie is stored by JavaScript as a document object. To create a cookie, we need to assign the name=value pair to the document.cookie property EXAMPLE: document.cookie=“id=“ + form1.cookie.value “; expires=“ + expiration_date + “;path=/”;

5 escape() and unescape() Built-In Functions – E1 The escape() and unescape() Methods URL Encoding function seeEncoding(form){ var myString = form.input.value; alert(escape(myString)); } function seeDecoding(form){ var myString = form.input.value; alert(unescape(myString)); } Type in a string of text:

6 Let’s Make a Cookie – E2 Making a Cookie function makeCookie(form){ var when = new Date(); when.setTime(when.getTime() + 24 * 60 * 60 * 1000); // 24 hours from now when.setFullYear(when.getFullYear() + 1); // One year from now yname=form.yourname.value; document.cookie=escape("name")+"="+escape(yname)+ ";expires="+when.toGMTString(); alert(document.cookie); } function welcome(myForm){ you=myForm.yourname.value; var position=document.cookie.indexOf("name="); if ( position != -1){ var begin = position + 5; var end=document.cookie.indexOf(";", begin); if(end == -1){ end=document.cookie.length;} you= unescape(document.cookie.substring(begin, end)); alert("Welcome " + you); } else{ alert("No cookies today");} } Got milk? What is your name?

7 Retrieving Cookies from a Server – E3 See my Cookies function seeCookie(){ if(document.cookie == ""){document.write("No cookies");} else{ var myCookie = document.cookie.split("; "); for(var i=0;i

8 Deleting a Cookie – E4 Delete Cookie var i = 0; function delCookie (cookieName){ document.cookie = cookieName + "=" +"; expires=Thu, 01-Jan :00:01 GMT"; alert("Cookie was deleted!"); seeCookie(); } function seeCookie(){ if(document.cookie == ""){ alert("No cookies"); return false; } else{ var myCookie = document.cookie.split("; "); if ( i < myCookie.length ){ document.form1.cookietype.value = myCookie[i].split("=")[0]; i++; // Increase the index value to see the next cookie } else{alert("No more cookies");} } Got milk? Is this the cookie you want to delete? Yes No


Download ppt "Cookies The HTTP protocol is stateless, that is, it does not maintain information on states after the session ends. But sometimes it is useful to remember."

Similar presentations


Ads by Google