Browser cookies are used to store small pieces of information on a client machine. A cookie can store only up to 4 KB of information. Generally cookies are used to store data which user types frequently such as user id and password to login to a site.
There are two types of browser cookies - session and persistent. Session cookies, also called as temporary cookies are stored in a browsers memory and stay alive as long as a browser session is live. When you close a browser, these cookies die.
On the other hand, persistent cookies stored on the hard drive with a date information and can live for a long time. When you create persistent cookies, you specify a date as the life period of the cookie. Even though it's not certain that a cookie will live as long as you specify.
Even though cookies are useful object in Web development but they've some limitations. As we said earlier, you can't store large data in cookies. Cookies are also browser dependent, which means your cookies based application may not work in some browsers.
You can specify a life period of a cookie, but there is no guarantee a cookie will be available for that long time. So storing important information in cookies is not a good idea.
Creating and Reading Cookies
The HttpCookie class defined in the System.Web namespace represents a browser cookie. The Cookies property of the Response and Request classes can be used to read all cookies as a HttpCookieCollection object, which represents a collection of cookies. Like other collection classes, the HttpCookieCollection class provides members to add, remove, and read cookies from the collection.
You can create a cookie by using the HttpCookie class by passing cookie name and its value as string values as first and second arguments. Or you can also use the Name and Value properties of HttpCookie to set the name and values of a cookie. The Expires property of the HttpCookie class makes a cookie persistent and sets a date when a cookie will expire.
The code listed in Listing 1 creates two cookies "UID" and "PASS". As you can see from this code, we add cookies to the collection by using the Response.Cookies.Add method.
Listing 1. Creating cookies
' Create user id and password cookies
' set their values and add to the collection
Dim cookie As HttpCookie = New HttpCookie("UID")
cookie.Value = "myid"
cookie.Expires = #9/28/2002#
Response.Cookies.Add(cookie)
cookie = New HttpCookie("PASS")
cookie.Value = "mypass"
cookie.Expires = #9/28/2002#
Response.Cookies.Add(cookie)
You can read cookies using the Request.Cookies property. The code listed in Listing 2 reeds the cookies from the browser and adds them to a ListBox control.
Listing 2. Reading cookies
' Read cookies
Dim cookieCols As New HttpCookieCollection
cookieCols = Request.Cookies
Dim str As String
' Read and add all cookies to the list box
For Each str In cookieCols
ListBox1.Items.Add("Cookie: " + str)
ListBox1.Items.Add("Value:" & _
Request.Cookies(str).Value)
Next
You can use the Remove and Clear methods of HttpCookieCollection to remove a particular cookie by name or all cookies respectively. The code listed in Listing 3 delete the cookies using the Remove method.
Listing 3. Deleting cookies
Dim
cookieCols As New HttpCookieCollection
cookieCols = Request.Cookies
Dim str As String
' Read and add all cookies to the list box
Request.Cookies.Remove("PASS")
Request.Cookies.Remove("UID")
A cookie can also store multiple values. This type of cookies called cookie dictionaries. You use Values property to create and read this type of cookies. The code listed in Listing 4 creates a dictionary cookie.
Listing 4. Creating a dictionary cookie
Dim
cookDict As HttpCookie = New HttpCookie("dict")
cookDict.Values("fname") = "first name"
cookDict.Values("lname") = "last name"
cookDict.Values("Address") = "address"
Response.Cookies.Add(cookDict)