ASP
Cookies
Like ASP Sessions, ASP Cookies
are used to store information
specific to a visitor of your
website. This cookie is stored to
the user's computer for an
extended amount of time. If you
set the expiration date of the
cookie for some day in the future
it will remain their until that
day unless manually deleted by the
user.
ASP Create Cookies
Creating an ASP cookie is
exactly the same process as
creating an ASP Session. Once
again, you must create a key/value
pair where the key will be the
name of our "created cookie". The
created cookie will store the
value which contains the actual
data.
In this example we will create
a cookie named brownies that
stores how many brownies we ate
during the day.
ASP Code:
<%
'create the cookie
Response.Cookies("brownies") =
13
%> |
Now that we've created this
cookie, how do we get that
information back from the user's
computer?
ASP Retrieving Cookies
To get the information we have
stored in the cookie we must use
the ASP Request Object that
provides a nice method for
retrieving cookies we have stored
on the user's computer. Below we
retrieve our cookie and print out
its value.
ASP Code:
<%
Dim myBrownie
'get the cookie
myBrownie =
Request.Cookies("brownies")
Response.Write("You ate " &
myBrownie & " brownies")
%> |
Display:
Note: Be sure you see that when
you create a cookie you use
Response.Cookies, but when you
retrieve a cookie you use
Request.Cookies.
ASP Cookie Expiration Date
Unlike real life cookies, in
ASP you can set how long you want
your cookies to stay fresh and
reside on the user's computer. A
cookie's expiration can hold a
date; this date will specify when
the cookie will be destroyed.
In our example below we create
a cookie that will be good for 10
days by first taking the current
date then adding 10 to it.
ASP Code:
<%
'create a 10-day cookie
Response.Cookies("brownies") =
13
Response.Cookies("brownies").Expires
= Date() + 10
'create a static date cookie
Response.Cookies("name") =
"Suzy Q."
Response.Cookies("name").Expires
= #January 1,2009#
%> |
ASP Cookie Arrays or
Collections
Up until now we have only been
able to store one variable into a
cookie, which is quite limiting if
you wanted to store a bunch of
information. However, if we make
this one variable into a
collection it can store a great
deal more. Below we make a
brownies collection that stores
all sorts of information.
ASP Code:
<%
'create a big cookie
Response.Cookies("brownies")("numberEaten")
= 13
Response.Cookies("brownies")("eater")
= "George"
Response.Cookies("brownies")("weight")
= 400
%> |
ASP Retrieving Cookie Values
From a Collection
Now to iterate through the
brownies collection we will use a
for each loop. See our for loop
tutorial for more information.
ASP Code:
<%
For Each key In
Request.Cookies("Brownies")
Response.Write("<br />" & key
& " = " & _
Request.Cookies("Brownies")(key))
Next
Response.Cookies("brownies")("numberEaten")
= 13
Response.Cookies("brownies")("eater")
= "George"
Response.Cookies("brownies")("weight")
= 400
%> |
Display:
numberEaten = 13
eater = George
weight = 400 |
|