posted 6/24/2009 by Vijendra Shakya
A cookie is a small bit of text file that browser creates and stores on your machine (hard drive). Cookie is a small piece of information stored as a string. Web server sends the cookie and browser stores it, next time server returns that cookie.Cookies are mostly used to store the information about the user. Cookies are stores on the client side.How to create the cookies:
//for store the values in the cookies Response.Cookies["MyName"].Value = "vijendra"; //how much time cookies save on the user hard drive Response.Cookies["MyName"].Expires = DateTime.Now.AddDays(10); // now this cookies save 10 days on the user hard drive.after 10 days this cookies expires.
if we do not add Response.Cookies["MyName"].Expires = DateTime.Now.AddDays(10); and we create the cookie in that case cookie will not store on the user hard drive, then it will stores on the browser, when we close the browser the cookie automatically Expires.How to Read the cookie:
When we want to read the cookies then first we check the cookie whether exit or not:
string myName=string.Empty; if(!string.IsNullOrEmpty(Request.Cookies["MyName"])) { // assign cookie value in the variable. myName = Request.Cookies["MyName"].Value; }
How to Delete the cookies:
First we check cookie exit or not:
if(!string.IsNullOrEmpty(Request.Cookies["MyName"])) { // Set its Expirey time in the past Response.Cookies["MyName"].Expires = DateTime.Now.AddDays(-1); }
Very nice Description. I was looking for the physical path of cookie and didnt get it anywhere buut got here... Thanks
This comment is awaiting moderation.
What kind of email newsletter would you prefer to receive from CodeAsp.Net?18