"Response.Cookies" 命令用于创建 cookie。
"Request.Cookies" 命令用于取回 cookie 的值。
在下面的例子中,我们会创建一个名为 "firstname" 的 cookie,并向其赋值 "Alex":
<% Response.Cookies("firstname")="Alex" %>
向 cookie 分配属性也是可以的,比如设置 cookie 的失效时间:
<% Response.Cookies("firstname")="Alex" Response.Cookies("firstname").Expires=#May 10,2020# %>
在下面的例子中,我们取回了名为 "firstname" 的 cookie 的值,并把值显示到了页面上:
<% fname=Request.Cookies("firstname") response.write("Firstname=" & fname) %>
输出:
Firstname=Alex
读取所有的 cookie
请阅读下面的代码:
<% Response.Cookies("firstname")="Alex" Response.Cookies("user")("firstname")="John" Response.Cookies("user")("lastname")="Adams" Response.Cookies("user")("country")="UK" Response.Cookies("user")("age")="25" %>
假设您的服务器将所有的这些 cookie 传给了某个用户。
现在,我们需要读取这些 cookie。下面的例子向您展示如何做到这一点(请注意,下面的代码会使用 HasKeys 检查 cookie 是否拥有键):
<html>
<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>
输出:
firstname=Alex user:firstname=John user:lastname=Adams user:country=UK user:age=25