文字
分享

ASP Cookies



cookie 常用于识别用户。


在线实例

Welcome cookie
本例演示如何创建 Welcome cookie。

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

<%

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

%>

<!DOCTYPE html>

<html>

<body>

</body>

</html>

1

Welcome! This is the first time you are visiting this Web page.


Cookie 是什么?

cookie 常用用于识别用户。cookie 是一种服务器留在用户计算机上的小文件。每当同一台计算机通过浏览器请求页面时,这台计算机将会发送 cookie。通过 ASP,您能够创建并取回 cookie 的值。


如何创建 Cookie?

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

注释:Response.Cookies 命令必须出现在 <html> 标签之前。

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

1

<div><%<br> Response.Cookies("firstname")="Alex"<br> %></div>

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

1

<div><%<br> Response.Cookies("firstname")="Alex"<br> Response.Cookies("firstname").Expires=#May 10,2012#<br> %></div>


如何取回 Cookie 的值?

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

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

1

<div><%<br> fname=Request.Cookies("firstname")<br> response.write("Firstname=" & fname)<br> %></div>

输出: Firstname=Alex


带有键的 Cookie

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

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

1

<div><%<br> Response.Cookies("user")("firstname")="John"<br> Response.Cookies("user")("lastname")="Smith"<br> Response.Cookies("user")("country")="Norway"<br> Response.Cookies("user")("age")="25"<br> %></div>


读取所有的 Cookie

请阅读下面的代码:

1

<div><%<br> Response.Cookies("firstname")="Alex"<br> Response.Cookies("user")("firstname")="John"<br> Response.Cookies("user")("lastname")="Smith"<br> Response.Cookies("user")("country")="Norway"<br> Response.Cookies("user")("age")="25"<br> %></div>

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

现在,我们需要读取这些传给某个用户的所有的 cookie。下面的实例向您演示了如何做到这一点(请注意,下面的代码通过 HasKeys 属性检查 cookie 是否带有键):

1

<div><!DOCTYPE html><br><html><br> <body><br><br> <%<br> dim x,y<br> for each x in Request.Cookies<br> response.write("<p>")<br> if Request.Cookies(x).HasKeys then<br> for each y in Request.Cookies(x)<br> response.write(x & ":" & y & "=" & Request.Cookies(x)(y))<br> response.write("<br>")<br> next<br> else<br> Response.Write(x & "=" & Request.Cookies(x) & "<br>")<br> end if<br> response.write "</p>"<br> next<br> %><br><br> </body><br> </html></div>

输出:

firstname=Alex

user:firstname=John
user:lastname=Smith
user:country=Norway
user:age=25


如果浏览器不支持 Cookie 该怎么办?

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

1. 向 URL 添加参数

您可以向 URL 添加参数:

1

<a href="welcome.asp?fname=John&lname=Smith">Go to Welcome Page</a>

然后在 "welcome.asp" 文件中取回这些值,如下所示:

1

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

2. 使用表单

您可以使用表单。当用户点击 Submit 按钮时,表单会把用户输入传给 "welcome.asp" :

1

<div><form method="post" action="welcome.asp"><br> First Name: <input type="text" name="fname" value=""><br> Last Name: <input type="text" name="lname" value=""><br> <input type="submit" value="Submit"><br> </form></div>

然后在 "welcome.asp" 文件中取回这些值,如下所示:

1

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