|
The Cookies collection sets the value of a cookie. If a specified cookie does not exist, it is created. If it does exist, the cookie takes on the new value and the old value is discarded.
Response.Cookies(cookie) [(key) | .attribute] = value
cookie
The name of the cookie.
key
Optional. If key is specified, the cookie is a dictionary and key is set to value.
attribute
Specific information about the cookie itself. The attribute can be one of the following:
Attribute |
Description |
Expires |
The date on which the cookie expires. This attribute must be set to a date later than the current date to store the cookie on the client disk after the current session ends. Write-only. |
HasKeys |
Indicates that the cookie has keys. Read-only. |
Path |
If set, the cookie is only sent to requests on this path. If the attribute is not set, the application path is used. Write-only. |
Secure |
Indicates that the cookie is secure. Write-only. |
value
Specifies the value to assign to key or attribute.
If a cookie with keys is created, as in the following script:
Response.Cookies("myCookie")("type1") = "sugar"
Response.Cookies("myCookie")("type2") = "ginger snap"
the following header is sent:
SET-COOKIE:MYCOOKIE=TYPE1=sugar&TYPE2=ginger+snap
Any subsequent assignment to myCookie that does not include a key would destroy type1 and type2. The following example discards the values type1 and type2 and replaces them with the value "chocolate chip":
Response.Cookies("myCookie") = "chocolate chip"
Conversely, calling a cookie with a key destroys any non-key values the cookie might contain. The following code will discard the value "chocolate chip" and insert the key value instead:
Response.Cookies("myCookie") ("NewType") = "peanut butter"
To check to see if a cookie has key values, use the following:
Response.Cookies("myCookie").HasKeys
If myCookie is a dictionary and has keys, the previous script will evaluate to TRUE, otherwise it will be FALSE.
You can use an iterator to set cookie attributes. The following example sets all the cookies in a collection to expire on Dec. 31, 1999:
<%
For each cookie in Response.Cookies
Response.Cookies(cookie).ExpiresAbsolute = #Dec. 31, 1999#
Next
%>
An iterator can also be used to set the values of all the cookies in a collection, or all the keys in a cookie. However, when using an iterator to retrieve cookie values, the cookies must have keys or the iterator will not execute. Use the HasKeys property to check to see whether a cookie has any keys. This is demonstrated in the following example.
<%
If Not cookie.HasKeys Then
'Set the value of the cookie
Response.Cookies(cookie) = ""
Else
'Set the value for each key in the cookie collection
For Each key in Response.Cookies(cookie)
Response.Cookies(cookie)(key) = ""
Next key
%>
The following example shows how you can set cookie values and their attributes:
<%
Response.Cookies("Type") = "Chocolate Chip"
Response.Cookies("Type").Expires = "July 31, 1997"
Response.Cookies("Type").Domain = "msn.com"
Response.Cookies("Type").Path = "/www/home/"
Response.Cookies("Type").Secure = FALSE
%>
Copyright 2002 Sun Microsystems, Inc. All rights reserved. Legal Notice.