Cookies have a much more complex value than other collections of ASP (such as form and ServerVariables). A cookie is a small piece of text that is stored on the client system by the browser and is sent with each request to a server in the domain to which they are applied.
ASP makes it easier to apply cookies by obtaining all the cookie values from the Request object's cookies collection and creating or modifying cookies, which are sent back to the user via the Response object's cookie collection.
Cookies contain information that can be constructed in two ways, and a single value cookie provides its value to code through a generic set of class ASP. However, each member of the collection may itself be a collection, and a cookie containing this information is called a multivalued (multiple-value) cookie.
Creating a single value cookie is simpler, as follows:
Response.Cookies(“item-name”) = “item-value”
To create a multiple-valued cookie, you can use the following command:
Response.Cookies(“item-name”)(“sub-item-name”) = “sub-item-value”
To set the domain and path of the cookie application and its expiration date, we use:
Response.Cookies(“item-name”).domain = “domain-url”
Response.Cookies(“item-name”).path = “virtual-path”
Response.Cookies(“item-name”).expires = #date#
Typically, a client sends a cookie to the server only when a request is made to a page in the directory where the cookie is created. By specifying the path attribute, you can specify where the cookie is valid in the site and the cookie will be sent with the request. If the cookie is sent with a page request for the entire site, set path to "/".
If the Expires property is not set, the cookie is automatically eliminated when the current browser instance is closed.
Note that when we send any output to the browser, we have created a cookie. Because these cookies are part of the page HTTP header.
In ASP 3.0, the default state of buffering is turned on and no output is sent unless the work is specified with Response.Flush or the page is to the end. This means that the code that creates the cookie can be executed anywhere on the page until any output "refresh" (flush) is made to the client.
To read an existing cookie, use the Request.Cookies collection. You can access items individually, similar to the methods that are used when you create them.
StrSingleValue = Request.Cookies(“item-name”)
StrSubItemValue = Request.Cookies(“item-name”)(“sub-item-name”)
Note that the Request.Cookies collection (like all other request collections) is read-only. The Response.Cookies collection is write-only and can actually access the names of a series of cookies in the collection, not their values.
Traversing Cookies Collection
To make it easier to use a set of cookies, you can use additional properties named HasKeys. If the access cookie itself is also a collection, that is, it is a multivalued cookie, this returns TRUE. Using the HasKeys property, you can traverse the complete Request.Cookies collection to get a list of all cookies and their values.
For Each objItem In Request.Cookies
If Request.Cookies(objItem).HasKey Then
‘Use another For Each to iterate all subkeys
For Each objItemKey in Request.Cookies(objItem)
Response.Write objItem & “(“ & objItemKey & “) = “_
& Request.Cookies(objItem)(objItemKey) & “<BR>”
Next
Else
‘Print out the cookie string as normal
Response.Write objItem & “ = ”& Request.Cookies(objItem) & “<BR>”
End If
Next
This is very similar to the previous complex code that extracts multiple values from the Request.Form collection. But here you can use the HasKeys property to determine whether each entry is a collection. In the Form example, you must query Request.Form (item_name). The Count property, because a form collection (and all other collections except cookies) cannot be a true collection. The ASP just did the "behind-the-scenes" work and got the value of each multiple-entry collection.