ASP COOKIE

"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



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值