ASP Cookies collection
Cookies collection is used to set or get the value of the cookie. If the cookie does not exist, create it, and give it a predetermined value.
Note: Response.Cookies command must precede the <html> tag.
grammar
variablename=Request.Cookies(name)[(key)|.attribute]
参数 | 描述 |
---|---|
name | 必需。cookie 的名称。 |
value | 必需(对于 Response.Cookies 命令)。cookie 的值。 |
attribute | 可选。规定有关 cookie 的信息。可以是下面的参数之一:
|
key | 可选。规定在何处赋值的 key。 |
Examples
"Response.Cookies" command is used to create a cookie or set cookie values:
Response.Cookies("firstname")="Alex"
%>
In the above code, we have created a directory named "firstname" the cookie, and assign it to "Alex".
You can also set the properties for the cookie, for example, you set the cookie expire:
Response.Cookies("firstname")="Alex"
Response.Cookies("firstname").Expires=#May 10,2002#
%>
Now, the value named "firstname" of the cookie is "Alex", while its expiration date in the user's computer is May 10, 2002.
"Request.Cookies" command is used to retrieve a cookie value.
In the following example, we retrieve the cookie "firstname" value, and displays it on the page:
fname=Request.Cookies("firstname")
response.write("Firstname=" & fname)
%>
Output:
A cookie can contain more than one set of values. We have called cookie key.
In the following example, we want to create a cookie collection named "user" of. "User" cookie has contains information about the user's key:
Response.Cookies("user")("firstname")="John"
Response.Cookies("user")("lastname")="Smith"
Response.Cookies("user")("country")="Norway"
Response.Cookies("user")("age")="25"
%>
The following code can be read cookie has been sent by the user for all servers. Note that we use HasKeys property to determine whether the cookie has key:
<body>
<%
dim x,y
for each x in Request.Cookies
response.write("<p>")
if Request.Cookies(x).HasKeys then
for each y in Request.Cookies(x)
response.write(x & ":" & y & "=" & Request.Cookies(x)(y))
response.write("<br /")
next
else
Response.Write(x & "=" & Request.Cookies(x) & "<br>")
end if
response.write "</p>"
next
%>
</body>
</html>
%>
Output:
user:firstname=John
user:lastname=Smith
user:
country=Norway
user:
age=25
Complete Response Object Reference