Cookie|cookies|object|response|cookie
Cookies
Cookies collection sets the value of the cookie. If the specified cookie does not exist, it is created. If it exists, the new value is set and the old value is deleted.
Grammar
Response.Cookies (cookie) [(key) |. Attribute] = value
Parameters
-
Cookies
-
The name of the cookie.
-
Key
-
Optional parameters. If a key is specified, the cookie is a dictionary and the key is set to value.
-
Attribute
-
Specifies information about the cookie itself. Attribute parameters can be one of the following: Name description domain is only allowed to write. If specified, the cookie is sent to the request in the domain. Expires only allowed to write. The expiration date of the cookie. To store cookies on the client disk after the session has ended, you must set the date. If the setting for this property does not exceed the current date, the cookie expires after the task ends. HasKeys is only allowed to read. Specifies whether the cookie contains keywords. Path only allows writing. If specified, the cookie is sent only to the request for that path. If this property is not set, the path to the application is used. Secure is only allowed to write. Specifies whether the cookie is safe.
-
Value
-
Specifies the value assigned to a key or attribute.
Comments
If you create a cookie with a keyword, as shown in the following script,
<% response.cookies ("MyCookie") ("type1") = "Sugar" response.cookies ("MyCookie") ("type2") = "ginger Snap"%>
Then this title will be sent out.
If you specify MyCookie without specifying a keyword, the type1 and type2 are destroyed. As shown in the following example.
<% response.cookies ("MyCookie") = "Chocolate Chip"%>
In the preceding example, the keywords type1 and type2 are corrupted and their values are also deleted. The MyCookie cookie has only the chocolate chip value.
Conversely, if you call a cookie with a keyword, it destroys any values that the cookie contains without a keyword. For example, if you call Response.Cookies with the following statement after the above code,
<% response.cookies ("MyCookie") ("newType") = "Peanut Butter"%>
The value of the chocolate chip will be deleted and NewType will be set to peanut butter.
To determine whether a cookie has keywords, use the following syntax.
<%= response.cookies ("MyCookie"). HasKeys%>
If MyCookie is a cookie dictionary, the previous value is TRUE. Otherwise, FALSE.
You can set the properties of a cookie by looping. For example, to set all cookies to expire on a specific date, you can use the following syntax.
<% for each cookie in Response.Cookies response.cookie (cookie). Expires = #July 4, 1997#next%>
You can use a loop to set all the cookies in a collection or all the keywords in a cookie. However, loops are not executed if the cookie is referenced without a keyword. To avoid this situation, you can use it first. HasKeys syntax checks whether a cookie has a keyword. This is illustrated 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% >
Example
The following example shows how to set the value of a cookie and how to assign a value to its properties.
<% response.cookies ("type") = "Chocolate Chip" response.cookies ("type"). Expires = "July, 1997" Response.Cookies ("Type"). Domain = "msn.com" Response.Cookies ("Type"). Path = "/www/home/" Response.Cookies ("Type"). Secure = false%>
Apply to
Response objects