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.
What are Cookies?
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.
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.
To read the value of a cookie, you need to use the
Cookies collection in the Request
object. Here is how you use it!
For example, if you had created a cookie named
FavoriteURL, you could display the value of
this cookie with:
If the cookie
FavoriteURL does not exist, a blank string will be returned. If you created
a cookie named
FavoriteURL with four keys,
could display each URL with the following code:
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:
If you do not set the
Expires property, the cookie will expire when the user closes
To set a cookie's value, simply use the following syntax:
To set a cookie's key values, use the following syntax:
So, if we wanted to create a our
FavoriteURL cookie with its four keys (
we could use the following syntax:
This cookie will expire in 30 days. There are a couple of other properties that you can set when using
cookies, such as
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
then the cookie will only be sent to the web server when communicating over a secure connection (HTTPS).
This property defaults to