课程表

VBScript 教程

VBScript 实例

VBScript 参考手册

ASP 教程

ASP 高级

ASP 组件

AJAX 与 ASP

ASP 参考/总结/实例

ADO 教程

ADO 对象

ADO 总结/实例

工具箱
速查手册

ASP Cookies

当前位置:免费教程 » 程序设计 » ASP/ADO/VBScript

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

输出:

  1. firstname=Alex
  2.  
  3. user:firstname=John
  4. user:lastname=Adams
  5. user:country=UK
  6. 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. %>
转载本站内容时,请务必注明来自W3xue,违者必究。
 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号