Using Cookies
The cookies collection stores values from any cookies that are sent
by the client to the server. The server needs to be able to write
information to cookies on the clients machine. ASP uses the Response
object's cookies collection to set cookies values.
Creating Cookies
The Cookies collection sets the value of a cookie. If the specified
cookie does not exist, it is created. If the cookie exists, it takes
the new value and the old value is discarded.
Syntax:
Response.Cookies(cookie)[(key)|.attribute]=value
parameters
cookie: The name of the cookie
key: An optional parameter. If key is specified,
cooke is a dictionary, and key is set to value.
attribute:Specifies information bout the cookie
itself. The attribute parameter can be one of the following.
-
Domain: Write-only. If Specified, the cookie is end only to requests
to this domain.
-
Expries: Write-only. The date on which the cookie
expires. This date must be set in order for the cookie to be
stored on the client's dish after the session ends. If this attribute
is not set to a date beyond the current date, the cookie will
expire when the session ends.
- HasKeys: Read-only. Specifies whether the cookie contains keys.
Path: Write-only. If specified, the cookie is sent only to requests
to this path. If this attribute is not set, the application
path is used.
- Secure: Write-only. Specifies whether the cookie is secure.
Value: Specifies the value to assign to key or
attribute.
Remarks
If a cookie with a key is created, as in the following script,
<%
Reponse.Cookies("mycookie")("type1")="sugar"
Reponse.Cookies("mycookie")("type2")="sugar"
%>
this header is sent
Set-Cookie: MYCOOKIE=TYPE1=sugar&TYPE2=ginger=sugar
A subsequent assignment to mycookie without specifying a key, would
destroy type1 and type2.
This is shown in the following example.
<% Response.Cookies("mycookies") = "chocolate chip" %>
In the preceding example, the keys type1 and type2 are destroyed
and their values are discarded. The mycookie cookie now has the value "chocolate
chip".
Conversely, if you call a cookie with a key, it destroys any nonkey
values the cookie contained. For example, if after the preceding
code you call Response.Cookies with the following
<% Reponse.Cookies("MyCookie")("newType")= " peanut
butter"%>
The value chocolate chip is discarded and newType would be set to
peanut butter.
To determine whether a cookie has keys, use the following syntax.
<%=ResPonse.Cookies("myCookie").HasKeys%>
If mycookie is a cookie dictionary, the preceding value is TRUE.
Otherwise, it is FALSE.
You can use an iterator to set cookie attributes. For example, to
set all of the cookies to expire on a particular date, use the following
syntax.
<%
For Each cookie in Reponse.Cookies
Response.Cookies(cookie).Expires = #July 4, 1998#
Next
%>
You can also use an iterator to set the values of all the cookies
in a collection, or all the keys in a cookie. However, the iterator,
when invoked on a cookie that does not have keys, does not execute.
To avoid this, you can first use the .HasKeys syntax to check whether
a cookie has nay keys. This is demonstrated in the following example.
<%
If not cookie.Has Keys then
'Set the value of the cookie
Response.Cookies(cookie)=""
else
'set the value for each key in the cooke collection
For each key in Response.Cookies(Cookie)
Response.Cookies(cookie)(key) = " "
Next key
%>
Example
The following examples demonstrate how you can set value for a cookie
and assign values to its attributes.
<%
Reponse.Cookies("Type") = "Chocolate chip"
Reponse.Cookies("type").Expires=#July 31, 1998#
Reponse.Cookies("type").Domain="kidslovepc.com"
Reponse.Cookies("type").path="/home/"
Reponse.Cookies("type").Secure=FALSE
%>
|