Thursday, November 10, 2011

State Management:Cookie

In This Post I will show You what is Cookie,How to use it and what is advantage and Limitation of cookie..

What is Cookie?

A cookie is a small bit of text that accompanies requests and pages as they go between the Web
server and browser. The cookie contains information the Web application can read whenever the user visits the site.Imagine that when users request a page from your site, www.Example.com, your application sends not just a page, but a cookie containing the date and time. When the user's browser gets the page, the browser also gets the cookie, which it stores in a folder on the user's hard disk.Later, the user requests a page from your site again. When the user enters the URL
www.contoso.com, the browser looks on the local hard disk for a cookie associated with the URL. If the cookie exists, the browser sends the cookie to your site along with the page request. Your
application can then determine the date and time that the user last visited the site. You might use the information to display a message to the user, check an expiration date, or perform any other useful function.

Limitation of Cookies?
Before we start on the mechanics of working with cookies, I will mention a couple of limitations of
working with cookies. Most browsers support cookies of up to 4096 bytes. That is plenty of space for storing a few values on the user's computers, but you would not want to try to store a dataset or some other potentially large piece of data in a cookie. More practically, you probably do not want to store a big collection of user information in a cookie. Instead, you would want to store a user number or other identifier. Then, when the user visits your site again, you would use the user ID to look up user details in a database. (But see Cookies and Security for some caveats about storing user information.)
Browsers also impose limitations on how many cookies your site can store on the user's computer. Most browsers allow only 20 cookies per site; if you try to store more, the oldest cookies are discarded. Some browsers also put an absolute limit, usually 300, on the number of cookies they will accept from all sites combined.

How To Add(write) Cookie...

Example
HttpCookie cookie = new HttpCookie("BackgroundColor");
cookie.Value = "green";
cookie.Expires = DateTime.Now.AddDays(1);
Response.Cookies.Add(cookie);

Read Cookie

string color=Request.Cookies["BackgroundColor"].Value;

Delete Cookie

HttpCookie cookie = new HttpCookie("BackgroundColor");
cookie.Value = "green";
cookie.Expires =DateTime.Now.AddDays(-1);
Response.Cookies.Add(cookie);


Store Multiple values in cookie and Read it.

HttpCookie userCookie = new HttpCookie("UserInfo");
userCookie["Country"] = "Italy";
userCookie["City"] = "Rome";
userCookie["Name"] = "Jones";
userCookie.Expires = DateTime.Now.AddDays(3);
Response.Cookies.Add(userCookie);
Label1.Text = "Cookie create successful!";
HttpCookie cookie = Request.Cookies["UserInfo"];
if (cookie != null)
{
string country = cookie["Country"];
string city = cookie["City"];
string name = cookie["Name"];
Label2.Text = "Cookie Found and read";
Label2.Text += "Name: " + name;
Label2.Text += "Country: " + country;
Label2.Text += "City: " + city;
}

0 comments:

Post a Comment