When you think ASP, think...
Recent Articles xml
All Articles
ASP.NET Articles
Related Web Technologies
User Tips!
Coding Tips
spgif spgif

Sample Chapters
JavaScript Tutorials
MSDN Communities Hub
Official Docs
Stump the SQL Guru!
XML Info
Author an Article
spgif spgif
ASP ASP.NET ASP FAQs Feedback topnav-right
Print this page.
Published: Tuesday, November 02, 1999

The Lowdown on Cookies

There's nothing I like more than my grandma's home made M&M cookies. Man, those are good. This article is about cookies, although not about those gooey cookies my grandma makes. Rather, this article is about small text files, saved on the client's computer, for customization purposes.

- continued -

What are Cookies?
Back when the World Wide Web was in its infancy, user-by-user customization was difficult to accomplish. An early attempt at providing customization was the use of cookies. Cookies are small text files written to the client's computer by a particular web site. These text files can then be read by scripts on the same web server that wrote those cookies. Since the cookies are written on the client's computer, specific information can be saved for each user. By storing information on the client's computer, the web server doesn't have to save this information- this is good. However, since the information is saved to a particular computer, if a particular user moves to a different computer, his or her information won't be accessible- this is bad.

Cookies are nice because they can be used to store small bits of user-specific information. For example, imagine that you wanted to allow your visitors to create a series of their favorite links on the start page of your site (My Yahoo! does this, as do a number of other customizable portals). You could create a Form into which the user could enter a series of URLs. You could then store these URLs on the user's computer using a cookie. Whenever a user visited your start page, you would check to see if the cookie that contained their favorite URLs existed- if it did, you would create HREF tags containing their favorite links.

Using Cookies:
Cookies can be created, read, and modified using client-side JavaScript code. Of course, if we want to make server-side decisions based upon the values in the cookies, this isn't too helpful a fact. Thankfully, ASP provides a collection to create, read and modify cookies.

Each cookie you create is represented by a small text file on the client's computer. If you want to save a large number of values, you can create a single cookie with multiple keys. This will place only one text file on the client's computer, and this text file will contain all of the values you specify with the keys. We'll examine how to create keys in a moment. First, though, let's look at how to read the value of a cookie.

Reading Cookies:
To read the value of a cookie, you need to use the Cookies collection in the Request object. Here is how you use it!

  Dim strCookieValue
  strCookieValue = Request.Cookies(cookieName)(keyName)

For example, if you had created a cookie named FavoriteURL, you could display the value of this cookie with:

  Response.Write Request.Cookies("FavoriteURL")

If the cookie FavoriteURL does not exist, a blank string will be returned. If you created a cookie named FavoriteURL with four keys, URL1 through URL4, you could display each URL with the following code:

  Response.Write Request.Cookies("FavoriteURL")("URL1") & "<BR>"
  Response.Write Request.Cookies("FavoriteURL")("URL2") & "<BR>"
  Response.Write Request.Cookies("FavoriteURL")("URL3") & "<BR>"
  Response.Write Request.Cookies("FavoriteURL")("URL4") & "<BR>"

Creating/Modifying Cookies
To create or modify cookies we need to use the Cookies collection of the Response object. When creating cookies, there are a number of optional properties we can specify. One important property to set is the Expires property. When a cookie expires, the text file representing the cookie is automatically deleted from the client's computer. So, if you want to have the cookie saved for a week, you can set the Expires property as follows:

  Response.Cookies(cookieName).Expires = Now() + 7

If you do not set the Expires property, the cookie will expire when the user closes her browser.

To set a cookie's value, simply use the following syntax:

  Response.Cookies(cookieName) = value

To set a cookie's key values, use the following syntax:

  Response.Cookies(cookieName)(keyName) = value

So, if we wanted to create a our FavoriteURL cookie with its four keys (URL1 through URL4), we could use the following syntax:

  Response.Cookies("FavoriteURL")("URL1") = "https://www.4guysfromrolla.com"
  Response.Cookies("FavoriteURL")("URL2") = "http://www.ASPMessageBoard.com"
  Response.Cookies("FavoriteURL")("URL3") = "http://www.LearnASP.com"
  Response.Cookies("FavoriteURL")("URL4") = "http://www.ASP101.com"
  Response.Cookies("FavoriteURL").Expires = Now() + 30

This cookie will expire in 30 days. There are a couple of other properties that you can set when using cookies, such as Domain and Path. Respectively, these properties determine what domain, can access the cookie values, and what path an ASP script must exist in to retrieve the value of a cookie. It's best to just leave these properties alone since they default to your current domain and root path. Another optional property is the Secure property. If this is set to True then the cookie will only be sent to the web server when communicating over a secure connection (HTTPS). This property defaults to False.

Well, there you have the lowdown on cookies! For a great article on how to use cookies to create a customized homepage, check out Christopher Miller's article Creating a Customized HomePage!

Happy Programming!

ASP.NET [1.x] [2.0] | ASPFAQs.com | Advertise | Feedback | Author an Article