Other Languages - How do I steal cookies? - Asked By N on 12-Aug-11 04:51 AM

Im required to : 

1) Create a simple webpage. Add cookies to your web page. Examine the cookies in your computer and edit,delete and add cookies. Try to do the same using JavaScript. 


2) Try to steal cookies. 



I need urgent help for this. How do the assignment as of required above? 


Please help. Thank you so much.
Jitendra Faye replied to N on 12-Aug-11 04:57 AM
For this you can use javascript-

Setting and deleting cookies with jQuery is really easy (especially in comparison to regular JavaScript) but this feature is not included in the jQuery core. For this we need a plug-in. This post shows how to set and get the value of cookies with jQuery.

First download the jQuery cookie plugin for here: http://plugins.jquery.com/project/Cookie

Set a cookie

Setting a cookie with jQuery is as simple as this, here we are creating a cookie called “example” with a value “demo”:

$.cookie("example", "demo");

This is a session cookie and will be destroy when user close his/her browser. To make the same cookie for suppose 7 days. We can do it like this:

$.cookie("example", "demo", { expires: 7 });

The above example will create the cookie at the root level. If you wanted to make it apply only to e.g. “/admin” and make it for 7 days you can do it like this:

$.cookie("example", "demo", { path: '/admin', expires: 7 });

Get the cookie’s value

Getting the cookie’s value is also very easy in jQuery. The following would alert the value of “example” cookie:

alert( $.cookie("example") );

Delete the cookie

And finally, to delete a cookie set its value to null.
Note:- Setting it to e.g. an empty string doesn’t remove it; it just clears the value.

$.cookie("example", null);
Hope this will help you.

N replied to Jitendra Faye on 12-Aug-11 05:05 AM
how do i write it in html?
Riley K replied to N on 12-Aug-11 06:07 AM
Here is the JavaScript way to Create Read Delete Cookie

Setting a Cookie

function SetCookie(name,value,days) {
 var now= new Date();
 var expDate = new Date();
 if (days==null || days==0) days=1;
 //create date after no of "days" from now
 expDate.setTime(now.getTime() + 3600000*24*days);
  
 //create cookie with name, value and expire date
 document.cookie=name+"="+escape(value)+";expires="+expDate.toUTCString();
}

Call the above function to set the cookie:

SetCookie("testCookie","testval",30);

The Cookie “testCookie” has the value “testval” and expires in 30 days.

Reading a Cookie


ReadCookie("testCookie"); // Returns value "testval"
  
function ReadCookie(name) {
 if (name == "") return "";
 var strCookie =" " + document.cookie;
 var idx = strCookie.indexOf(" " + name + "=");
 if (idx == -1) idx = strCookie.indexOf(";" + name + "=");
 if (idx == -1) return "";
  
 var idx1 = strCookie.indexOf(";", idx+1);
 if (idx1 == -1) idx1 = strCookie.length;
 return unescape(strCookie.substring(idx + name.length+2, idx1));
}

Deleting a Cookie

To delete a cookie with a given name, set a cookie that has the same name and an expiration date in the past.

SetCookie("testCookie","",-1);

Regards
Jitendra Faye replied to N on 12-Aug-11 09:07 AM
If you want to use jquery then  First download the jQuery cookie plugin for here:

 http://plugins.jquery.com/project/Cookie


Then after you can use in html file like this-

Setting a cookie with jQuery is as simple as this, here we are creating a cookie called “example” with a value “demo”:

$.cookie("example", "demo");
Try this and let me know.