Wednesday, September 24, 2008

Understanding Cookies


A Cookie is a small text file that the browser creates and stores on the hard drive of your machine. Cookie is just one or more pieces of information stored as text strings. A Web server sends you a cookie and the browser stores it. The browser then returns the cookie to the server the next time the page is referenced. The most common use of a cookie is to store information about the user and preferences the user makes. For example, assume you like DVD's and register with Ebay to participate in online auctions. You are required to fill out a form with your name, credit card details and address. Ebay assigns you an ID, stores your information with that ID in its database on the server, and sends the ID to your browser as a cookie. Your browser stores the ID on your hard disk. The next time you go to Ebay, the ID is sent back to the server. The server looks you up by your ID and customizes the Web page it sends back to you. The page might say, "Cheap rates on your favorite DVD's".

Creating cookies with ASP.NET is simple and straight forward. The System.Web namespace offers a class called HttpCookie to create cookies. The following code demonstrates the creation of cookies. The code assumes that you have a TextBox, two Buttons and a RadioButtonList control with some items in it on a Web Forms page. Check the live code demo at the bottom of this page for more.

Private Sub Select_Click(ByVal sender As System.Object, ByVal e As_

System.EventArgs) Handles Select.Click

Dim newCookie As HttpCookie = New HttpCookie("Books")

newCookie.Values.Add("Name", TextBox1.Text)

newCookie.Values.Add("FavBook", RadioButtonList1.SelectedItem.Text)

newCookie.Expires = #12/31/2008#


Label3.Text = "Cookie Created"

Select.Visible = False

TextBox1.Visible = False

Label1.Visible = False

Label2.Visible = False

RadioButtonList1.Visible = False

End Sub

Understanding the Code

First, we created a variable called newCookie and an HttpCookie object is instantiated. This object enables us to pass a string when we create it to identify the name that the browser will use to store the cookie. We called it Books.

After we created the object, we added information to it using the Values property and the Add method.

To ensure that the cookie stays on the user's hard drive for a while we set it's date using the HttpCookie object's Expires property.

To get this cookie sent back to the browser and stored on the hard drive we used the Response object.

The code listing above creates and stores the cookie on the user's hard drive which is fine. To retrieve them at a later date, what should be done?

Retriving the cookie

The code below demonstrates how to retrieve a cookie and display information to the user based on his preferences. 

Private Sub Retrieve_Click(ByVal sender As System.Object, ByVal e As_

System.EventArgs) Handles Retrieve.Click


Label4.Text = "Hello" &" "& Request.Cookies("Books")("Name") & "."&_

"We have a new book for you:"

If Request.Cookies("Books")("FavBook") = "VB" Then

Label5.text="XYZ VB Book"

ElseIf Request.Cookies("Books")("FavBook") = "C#" Then

Label5.text="ABC C# Book"


Label5.text="Ultimate Solution "

End If

End Sub

Understanding the Code

First, we used the Request object to retrieve the cookie and display a message to the user.

Based on the user's selection first time he accessed the page, we display a new book to him using  If..Then..Else conditional.

The path to the location on the hard drive where cookies are stored is

C:\Documents and Settings\Administrator\Cookies.

No comments: