ASP Cookie

cookie 常用来对用户进行识别。


实例

Welcome cookie

如何创建欢迎 cookie。


什么是 Cookie?

cookie 常用来对用户进行识别。cookie 是一种服务器留在用户电脑中的小文件。每当同一台电脑通过浏览器请求页面时,这台电脑也会发送 cookie。通过 ASP,您能够创建并取回 cookie 的值。


如何创建 cookie?

"Response.Cookies" 命令用于创建 cookie。

注意:Response.Cookies 命令必须位于 <html> 标签之前。在下面的例子中,我们会创建一个名为 "firstname" 的 cookie,并向其赋值 "Alex":

  1. <%
  2. Response.Cookies("firstname")="Alex"
  3. %>

向 cookie 分配属性也是可以的,比如设置 cookie 的失效时间:

  1. <%
  2. Response.Cookies("firstname")="Alex";
  3. Response.Cookies("firstname").Expires=#May 10,2020#
  4. %>

如何取回 cookie 的值?

"Request.Cookies" 命令用于取回 cookie 的值。

在下面的例子中,我们取回了名为 "firstname" 的 cookie 的值,并把值显示到了页面上:

  1. <%
  2. fname=Request.Cookies("firstname")
  3. response.write("Firstname=" &amp; fname)
  4. %>

输出:

  1. Firstname=Alex

带有键的 cookie

如果一个 cookie 包含多个值的一个集合,我们就可以说 cookie 拥有键(Keys)。

在下面的例子中,我们会创建一个名为 "user" 的 cookie 集。"user" cookie 拥有包含用户信息的键:

  1. <%
  2. Response.Cookies("user")("firstname")="John"
  3. Response.Cookies("user")("lastname")="Adams"
  4. Response.Cookies("user")("country")="UK"
  5. Response.Cookies("user")("age")="25"
  6. %>

读取所有的 cookie

请阅读下面的代码:

  1. <%
  2. Response.Cookies("firstname")="Alex"
  3. Response.Cookies("user")("firstname")="John"
  4. Response.Cookies("user")("lastname")="Adams"
  5. Response.Cookies("user")("country")="UK"
  6. Response.Cookies("user")("age")="25"
  7. %>

假设您的服务器将所有的这些 cookie 传给了某个用户。

现在,我们需要读取这些 cookie。下面的例子向您展示如何做到这一点(请注意,下面的代码会使用 HasKeys 检查 cookie 是否拥有键):

  1. <html>
  2. <body>
  3. <%
  4. dim x,y
  5. for each x in Request.Cookies
  6. response.write("<p>")
  7. if Request.Cookies(x).HasKeys then
  8. for each y in Request.Cookies(x)
  9. response.write(x & ":" & y & "=" & Request.Cookies(x)(y))
  10. response.write("<br />")
  11. next
  12. else
  13. Response.Write(x & "=" & Request.Cookies(x) & "<br />")
  14. end if
  15. response.write "</p>"
  16. next
  17. %>
  18. </body>
  19. </html>

输出:

  1. firstname=Alex
  2. user:firstname=John
  3. user:lastname=Adams
  4. user:country=UK
  5. user:age=25

如何应对不支持 cookie 的浏览器?

如果您的应用程序需要和不支持 cookie 的浏览器打交道,那么您不得不使用其他的办法在您的应用程序中的页面之间传递信息。这里有两种办法:

1. 向 URL 添加参数

您可以向 URL 添加参数:

  1. <a href="welcome.asp?fname=John&lname=Adams">
  2. Go to Welcome Page
  3. </a>

然后在类似于下面这个 "welcome.asp" 文件中取回这些值:

  1. <%
  2. fname=Request.querystring("fname")
  3. lname=Request.querystring("lname")
  4. response.write("<p>Hello " & fname & " " & lname & "!</p>")
  5. response.write("<p>Welcome to my Web site!</p>")
  6. %>
2. 使用表单

您还可以使用表单。当用户点击提交按钮时,表单会把用户输入的数据提交给 "welcome.asp" :

  1. <form method="post" action="welcome.asp">
  2. First Name: <input type="text" name="fname" value="">
  3. Last Name: <input type="text" name="lname" value="">
  4. <input type="submit" value="Submit">
  5. </form>

然后在 "welcome.asp" 文件中取回这些值,就像这样:

  1. <%
  2. fname=Request.form("fname")
  3. lname=Request.form("lname")
  4. response.write("<p>Hello " & fname & " " & lname & "!</p>")
  5. response.write("<p>Welcome to my Web site!</p>")
  6. %>

分类导航