当前位置: 首页 > 图文教程 > 网络编程 > ASP > ASP基础教程之ASP程序对Cookie的处理

ASP
ASP 编程中20个非常有用的例子(一)
ASP 编程中20个非常有用的例子(二)
ASP基础教程:ADO存取数据库时如何分页显示
ASP基础教程:其它的ASP常用组件
ASP基础教程:学习ASP中子程序的应用
ASP基础教程之ASP程序对Cookie的处理
ASP基础教程之实例学习ASP Response 对象
ASP基础教程之ASP AdRotator 组件的使用
ADO初学者教程:ADO 通过GetString()加速脚本
初学者来认识OLEDB和ODBC的区别
ASP常见数学函数 Abs Atn Cos 等详细详解
VBScript新手入门初学教程:VBScript简介
有用的无声递交表单的客户端函数
Windows 2003 安装设置iis
ASP技巧实例:几行代码解决防止表单重复提交
ASP读sql数据时出现乱码问题的解决方法
ASP技巧实例:使用ASP记录在线用户的数量
ASP技巧实例:关于对表单操作的程序
ASP技巧实例:ASP实现最简洁的多重查询的解决方案
ASP实例:利用缓存提高数据显示效率

ASP基础教程之ASP程序对Cookie的处理


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-08-14   浏览: 41 ::
收藏到网摘: n/a

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

实例:

以下为引用的内容:

<%
dim numvisits
response.cookies("NumVisits").Expires=date+365
numvisits=request.cookies("NumVisits")
if numvisits="" then
   response.cookies("NumVisits")=1
   response.write("Welcome! This is the first time you are visiting this Web page.")
else
   response.cookies("NumVisits")=numvisits+1
   response.write("You have visited this ")
   response.write("Web page " & numvisits)
   if numvisits=1 then
     response.write " time before!"
   else
     response.write " times before!"
   end if
end if
%>
<html>
<body>
</body>
</html>

什么是Cookie?

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

如何创建cookie?

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

注意:Response.Cookies命令必须位于<html>标签之前。

在下面的例子中,我们会创建一个名为"firstname"的cookie,并向其赋值"Alex":

以下为引用的内容:
<%
Response.Cookies("firstname")="Alex"
%>

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

以下为引用的内容:
<%
Response.Cookies("firstname")="Alex"
Response.Cookies("firstname").Expires=#May 10,2002#
%>

如何取回cookie的值?

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

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

以下为引用的内容:
<%
fname=Request.Cookies("firstname")
response.write("Firstname=" & fname)
%>

输出:

Firstname=Alex

带有键的cookie

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

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

以下为引用的内容:
<%
Response.Cookies("user")("firstname")="John"
Response.Cookies("user")("lastname")="Smith"
Response.Cookies("user")("country")="Norway"
Response.Cookies("user")("age")="25"
%>

读取所有的cookie

请阅读下面的代码:

以下为引用的内容:
<%
Response.Cookies("firstname")="Alex"
Response.Cookies("user")("firstname")="John"
Response.Cookies("user")("lastname")="Smith"
Response.Cookies("user")("country")="Norway"
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=Smith
user:country=Norway
user:age=25

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

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

1. 向URL添加参数

你可以向URL添加参数:

以下为引用的内容:
<a href="welcome.asp?fname=John&lname=Smith">
Go to Welcome Page</a>

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

以下为引用的内容:
<%
fname=Request.querystring("fname")
lname=Request.querystring("lname")
response.write("<p>Hello " & fname & " " & lname & "!</p>")
response.write("<p>Welcome to my Web site!</p>")
%>

2. 使用表单

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

以下为引用的内容:
<form method="post" action="welcome.asp">
First Name:  <input type="text" name="fname" value="">
Last Name: <input type="text" name="lname" value="">
<input type="submit" value="Submit">
</form>

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

以下为引用的内容:

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