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.
Response.Cookies(cookie)[(key)|.attribute] = value
|Domain||Write-only. If specified, the cookie is sent only to requests to this domain.|
|Expires||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 disk 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.|
If a cookie with a key is created, as in the following script,
<% Response.Cookies("mycookie")("type1") = "sugar" Response.Cookies("mycookie")("type2") = "ginger snap" %>
this header is sent.
A subsequent assignment to
myCookie without specifying a key, would destroy
type2. This is shown in the following example.
<% Response.Cookies("myCookie") = "chocolate chip" %>
In the preceding example, the keys
type2 are destroyed and their values are discarded. The
myCookie cookie now has the value
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
<% Response.Cookies("myCookie")("newType") = "peanut butter" %>
chocolate chip is discarded and
newType would be set to
To determine whether a cookie has keys, use the following syntax.
<%= Response.Cookies("myCookie").HasKeys %>
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 Response.Cookies Response.Cookie(cookie).Expires = #July 4, 1997# 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 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 examples demonstrate how you can set a value for a cookie and assign values to its 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 %>