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



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
/* 豆腐制作 都是精品 www.asp888.net 豆腐技术站 如果转载 请添加这个 版权信息 */ [removed] Const COOKIE_NAME As String = "豆腐站点" Const COOKIE_VALUE As String = "http://www.asp888.net" ' 声明 Cookie 对象 Dim objCookieObject As HttpCookie Sub btnSetCookie_OnClick(Sender As Object, E As EventArgs) ' Create a cookie object - I'm passing name and value, ' but you can also pass in a name and set the value later. objCookieObject = New HttpCookie(COOKIE_NAME, COOKIE_VALUE) ' 另外的一种操作Cookie 的 方法 'objCookieObject = New HttpCookie(COOKIE_NAME) 'objCookieObject.Name = COOKIE_NAME 'objCookieObject.Value = "sdsd" ' 设置Cookie 的 过期时间 2001/12/31 23:59:59 objCookieObject.Expires = New DateTime(2001, 12, 11, 23, 59, 59) ' 下面的这些可以不使用 objCookieObject.Domain = "www.asp888.net" objCookieObject.Path = "/path/" objCookieObject.Secure = True Response.AppendCookie(objCookieObject) End Sub Sub btnRemoveCookie_OnClick(Sender As Object, E As EventArgs) objCookieObject = New HttpCookie(COOKIE_NAME) ' 删除Cookie objCookieObject.Expires = New DateTime(1974, 11, 12) Response.AppendCookie(objCookieObject) End Sub Sub btnGetCookie_OnClick(Sender As Object, E As EventArgs) objCookieObject = Request.Cookies(COOKIE_NAME) If Not(objCookieObject = null) Then lblCookieDetails.Text = objCookieObject.Name lblCookieDetailsName.Text = objCookieObject.Name lblCookieDetailsValue.Text = objCookieObject.Value lblCookieDetailsExpires.Text = objCookieObject.Expires.ToString lblCookieDetailsDomain.Text = objCookieObject.Domain lblCookieDetailsPath.Text = objCookieObject.Path lblCookieDetailsSecure.Text = objCookieObject.Secure.ToString lblCookieDetailsHasKeys.Text = objCookieObject.HasKeys.ToString Else lblCookieDetails.Text = "Cookie Not Set!" lblCookieDetailsName.Text = "" lblCookieDetailsValue.Text = "" lblCookieDetailsExpires.Text = "" lblCookieDetailsDomain.Text = "" lblCookieDetailsPath.Text = "" lblCookieDetailsSecure.Text = "" lblCookieDetailsHasKeys.Text = "" End If End Sub [removed] <html> <head>

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值