我自己曾想写一篇关于Cookie的文章,特别是Client端Script同Server端ASP通过Cookie交互的问题可能会困扰大家。其实是如
果你对Cookie有深入的理解,特别是对域和路径的概念比较清晰的话,就不会有问题了。
另外想提示的一点是request.ServerVariables( " HTTP_COOKIE " )得到Cookie和Request.Cookies得到的结果可能会有所不同。
大家思考一下为什么,会对ASP的学习提高有所帮助的。
Request.Cookies,粒子:
< TABLE BORDER = " 2 " >
< THEAD >
< TH > Cookie Name </ TH >
< TH > Cookie Value </ TH >
< TH > Cookie HasKeys </ TH >
</ THEAD >
< %
Dim Item
For Each Item in Request.Cookies
% >
< TR >
< TD >< % = Item % ></ TD >
< TD >< % = Request.Cookies(Item) % ></ TD >
< TD >< % = Request.Cookies(Item).HasKeys % ></ TD >
</ TR >
< TR >
< %
If Request.Cookies(Item).HasKeys Then
For Each strSubKey In Request.Cookies(Item)
% >
< TD >& bnsp; </ TD >
< TD >& bnsp; </ TD >
< TD >< % = Request.Cookies(strKey)(strSubKey) % ></ TD >
< %
Next
End If
Next
% >
</ TABLE >
request.ServerVariables( " HTTP_COOKIE " ),粒子:
< TABLE BORDER = " 2 " >
< THEAD >
< TH > Cookie Name </ TH >
< TH > Cookie Value </ TH >
</ THEAD >
< %
Dim Item,sp,i,d
sp = split (request.ServerVariables( " HTTP_COOKIE " ), " ; " , - 1 , 1 )
' Loop through the cookie collection displaying each cookie we find
For i = 0 to UBound (sp)
d = split ( cstr (sp(i)), " = " , - 1 , 1 )
% >
< TR >
< TD >< % = d( 0 ) % ></ TD >
< TD >< % if UBound (d) = 1 then Response.Write(d( 1 )) else Response.Write " " % ></ TD >
</ TR >
< %
Next
% >
</ TABLE >
重粒子@Y2K0814
--------------------------------------------------------
果你对Cookie有深入的理解,特别是对域和路径的概念比较清晰的话,就不会有问题了。
另外想提示的一点是request.ServerVariables( " HTTP_COOKIE " )得到Cookie和Request.Cookies得到的结果可能会有所不同。
大家思考一下为什么,会对ASP的学习提高有所帮助的。
Request.Cookies,粒子:
< TABLE BORDER = " 2 " >
< THEAD >
< TH > Cookie Name </ TH >
< TH > Cookie Value </ TH >
< TH > Cookie HasKeys </ TH >
</ THEAD >
< %
Dim Item
For Each Item in Request.Cookies
% >
< TR >
< TD >< % = Item % ></ TD >
< TD >< % = Request.Cookies(Item) % ></ TD >
< TD >< % = Request.Cookies(Item).HasKeys % ></ TD >
</ TR >
< TR >
< %
If Request.Cookies(Item).HasKeys Then
For Each strSubKey In Request.Cookies(Item)
% >
< TD >& bnsp; </ TD >
< TD >& bnsp; </ TD >
< TD >< % = Request.Cookies(strKey)(strSubKey) % ></ TD >
< %
Next
End If
Next
% >
</ TABLE >
request.ServerVariables( " HTTP_COOKIE " ),粒子:
< TABLE BORDER = " 2 " >
< THEAD >
< TH > Cookie Name </ TH >
< TH > Cookie Value </ TH >
</ THEAD >
< %
Dim Item,sp,i,d
sp = split (request.ServerVariables( " HTTP_COOKIE " ), " ; " , - 1 , 1 )
' Loop through the cookie collection displaying each cookie we find
For i = 0 to UBound (sp)
d = split ( cstr (sp(i)), " = " , - 1 , 1 )
% >
< TR >
< TD >< % = d( 0 ) % ></ TD >
< TD >< % if UBound (d) = 1 then Response.Write(d( 1 )) else Response.Write " " % ></ TD >
</ TR >
< %
Next
% >
</ TABLE >
重粒子@Y2K0814
--------------------------------------------------------
下面是甘冀平翻译的
<
ASP中Cookie使用指南
>
原文出处:http: // www.asptoday.com / articles / 19990915 .htm
实际上,在web开发中,cookie仅仅是一个文本文件,当用户访问站点时,它就被存储在用户使用的计算机上,其中,保存了
一些信息,当用户日后再次访问这个站点时,web可以将这些信息提取出来。
尽管现在听起来cookie没有什么激动人心的,但实际上利用它,你能实现许多有意义的功能!比如说:你可以在站点上放置
一个调查问答表,询问访问者最喜欢的颜色和字体,然后根据这些定制用户的web界面。并且,你还可以保存访问者的登录密码,这
样,当访问者再次访问这个站点时,不用再输入密码进行登录。
当然,cookie也有一些不足。首先,由于利用cookie的功能可以编程实现一些不良企图,所以大多数的浏览器中都有安全设
定,其中可以设置是否允许或者接受cookie,因此这就不能保证随时能使用cookie。再者,访问者可能有意或者无意地删除
cookie。当访问者的机器遇到“蓝屏”死机时,或者重新格式化硬盘、安装系统后,原来保存的cookie将全部丢失。最后一点,有
一些最初始的浏览器并不能支持cookie。
利用cooklie能做什么?
有2种使用cookie的基本方式:
1 、将cookie写入访问者的计算机(使用 RESPONSE 命令)
2 、从访问者的计算机中取回cookie(使用 REQUEST 命令)
创建cookie的基本语法
Response.Cookies( " CookieName " ) = value
执行下面的代码将会在访问者的计算机中创建一个cookie,名字=VisitorName,值=Ken
Response.Cookies( " VisitorName " ) = " Ken "
执行下面的代码将会在访问者的计算机中创建一个cookie,名字=VisitorName,值=表单中UserName的值
Response.Cookies( " VisitorName " ) = Request.Form( " UserName " )
原文出处:http: // www.asptoday.com / articles / 19990915 .htm
实际上,在web开发中,cookie仅仅是一个文本文件,当用户访问站点时,它就被存储在用户使用的计算机上,其中,保存了
一些信息,当用户日后再次访问这个站点时,web可以将这些信息提取出来。
尽管现在听起来cookie没有什么激动人心的,但实际上利用它,你能实现许多有意义的功能!比如说:你可以在站点上放置
一个调查问答表,询问访问者最喜欢的颜色和字体,然后根据这些定制用户的web界面。并且,你还可以保存访问者的登录密码,这
样,当访问者再次访问这个站点时,不用再输入密码进行登录。
当然,cookie也有一些不足。首先,由于利用cookie的功能可以编程实现一些不良企图,所以大多数的浏览器中都有安全设
定,其中可以设置是否允许或者接受cookie,因此这就不能保证随时能使用cookie。再者,访问者可能有意或者无意地删除
cookie。当访问者的机器遇到“蓝屏”死机时,或者重新格式化硬盘、安装系统后,原来保存的cookie将全部丢失。最后一点,有
一些最初始的浏览器并不能支持cookie。
利用cooklie能做什么?
有2种使用cookie的基本方式:
1 、将cookie写入访问者的计算机(使用 RESPONSE 命令)
2 、从访问者的计算机中取回cookie(使用 REQUEST 命令)
创建cookie的基本语法
Response.Cookies( " CookieName " ) = value
执行下面的代码将会在访问者的计算机中创建一个cookie,名字=VisitorName,值=Ken
Response.Cookies( " VisitorName " ) = " Ken "
执行下面的代码将会在访问者的计算机中创建一个cookie,名字=VisitorName,值=表单中UserName的值
Response.Cookies( " VisitorName " ) = Request.Form( " UserName " )
读取cookie的基本语法
Request.Cookies( " CookieName " )
可以将Request值当作一个变量看待,执行下面的代码,将取回名字为KensCookie的cookie值,并存入变量MyVar:
MyVar = Request.Cookies( " KensCookie " )
执行下面的代码,将判断名字为KensCookie的cookie值是否为“Yes”:
If Request.Cookies( " KensCookie " ) = " Yes " then
功能丰富的cookie
你可以扩展上面的代码成为Cookie子关键值(CookieSubName),代码如下:
Response.Cookies( " VisitorName " )( " FirstName " ) = " Ken "
Response.Cookies( " VisitorName " )( " LastName " ) = " Baumbach "
讲解例子前,最后讨论2个概念:命令约定和使用到期时间。
命名约定
同其他变量的命名一样,合适地、独特地命名cookie,有利于在程序中前后连贯地使用它。你可以使用下面的1个或者2个
cookie属性进行cookie变量的命名:
域属性(Domain):域属性表明cookie由哪个网站产生或者读取,默认情况下,cookie的域属性设置为产生它的网站,但你
也可以根据需要改变它。相关代码如下:Response.Cookies( " CookieName " ).Domain = " www.mydomain.com "
路径属性(Path):路径属性可以实现更多的安全要求,通过设置网站上精确的路径,就能限制cookie的使用范围。例如:
Response.Cookies( " CookieName " ).Path = " /maindir/subdir/path "
使用到期时间
通常情况下,当浏览器关闭时,一个cookie就不存在了。但是在许多时候,比如下面将要讨论的web站点例子,我们希望能更
长时间地在访问者的计算机上保存cookie。很幸运,有这样的实现方法。下面的代码,就可以设置cookie的使用到期时间为2010年
1月1日:
Response.Cookies( " CookieName " ).Expires = #January 01 , 2010 #
Request.Cookies( " CookieName " )
可以将Request值当作一个变量看待,执行下面的代码,将取回名字为KensCookie的cookie值,并存入变量MyVar:
MyVar = Request.Cookies( " KensCookie " )
执行下面的代码,将判断名字为KensCookie的cookie值是否为“Yes”:
If Request.Cookies( " KensCookie " ) = " Yes " then
功能丰富的cookie
你可以扩展上面的代码成为Cookie子关键值(CookieSubName),代码如下:
Response.Cookies( " VisitorName " )( " FirstName " ) = " Ken "
Response.Cookies( " VisitorName " )( " LastName " ) = " Baumbach "
讲解例子前,最后讨论2个概念:命令约定和使用到期时间。
命名约定
同其他变量的命名一样,合适地、独特地命名cookie,有利于在程序中前后连贯地使用它。你可以使用下面的1个或者2个
cookie属性进行cookie变量的命名:
域属性(Domain):域属性表明cookie由哪个网站产生或者读取,默认情况下,cookie的域属性设置为产生它的网站,但你
也可以根据需要改变它。相关代码如下:Response.Cookies( " CookieName " ).Domain = " www.mydomain.com "
路径属性(Path):路径属性可以实现更多的安全要求,通过设置网站上精确的路径,就能限制cookie的使用范围。例如:
Response.Cookies( " CookieName " ).Path = " /maindir/subdir/path "
使用到期时间
通常情况下,当浏览器关闭时,一个cookie就不存在了。但是在许多时候,比如下面将要讨论的web站点例子,我们希望能更
长时间地在访问者的计算机上保存cookie。很幸运,有这样的实现方法。下面的代码,就可以设置cookie的使用到期时间为2010年
1月1日:
Response.Cookies( " CookieName " ).Expires = #January 01 , 2010 #
执行下面的代码,将设定cookie的过期时间为“cookie的创建时间+365日”:
Response.Cookies( " CookieName " ) = Date + 365
使用cookie的实际例子(非常精彩)
现在开始讨论实际的例子。假设:你想做一个调查,每个人初次访问时需要填写好信息,但是当日后再访问时,就不需要再那
么做。利用cookie,就可以非常圆满地解决这个问题,而大可不必用到数据库。
< %@ LANGUAGE = " VBSCRIPT " % >
< %
Survey = Request.Cookies( " KensSurvey " )
If Survey = "" then
Response.Cookies( " KensSurvey " ) = " x "
Response.Cookies( " KensSurvey " ).Expires = #January 01 , 2010 #
Response.Redirect " survey.asp "
Else
' rest of the page
End if
% >
好,下面开始从头讨论上面的代码。
Response.Cookies( " CookieName " ) = Date + 365
使用cookie的实际例子(非常精彩)
现在开始讨论实际的例子。假设:你想做一个调查,每个人初次访问时需要填写好信息,但是当日后再访问时,就不需要再那
么做。利用cookie,就可以非常圆满地解决这个问题,而大可不必用到数据库。
< %@ LANGUAGE = " VBSCRIPT " % >
< %
Survey = Request.Cookies( " KensSurvey " )
If Survey = "" then
Response.Cookies( " KensSurvey " ) = " x "
Response.Cookies( " KensSurvey " ).Expires = #January 01 , 2010 #
Response.Redirect " survey.asp "
Else
' rest of the page
End if
% >
好,下面开始从头讨论上面的代码。
首先,初始设置页面,并读取名字为KensSurvey的cookie值:
< %@ LANGUAGE = " VBSCRIPT " % >
< %
Survey = Request.Cookies( " KensSurvey " )
然后,判断是否已经存在cookie值:
If Survey = "" then
如果不存在, 就创建并设置cookie,并转到页面survey.asp。 当下一次访问时,因为存在cookie值,就不会再转到
survey.asp 页面。
Response.Cookies( " KensSurvey " ) = " x "
Response.Cookies( " KensSurvey " ).Expires = #January 01 , 2010 #
Response.Redirect " survey.asp "
如果cookie已经存在,那么访问者将执行页面中剩余的代码:
' rest of the page
End if
% >
< %@ LANGUAGE = " VBSCRIPT " % >
< %
Survey = Request.Cookies( " KensSurvey " )
然后,判断是否已经存在cookie值:
If Survey = "" then
如果不存在, 就创建并设置cookie,并转到页面survey.asp。 当下一次访问时,因为存在cookie值,就不会再转到
survey.asp 页面。
Response.Cookies( " KensSurvey " ) = " x "
Response.Cookies( " KensSurvey " ).Expires = #January 01 , 2010 #
Response.Redirect " survey.asp "
如果cookie已经存在,那么访问者将执行页面中剩余的代码:
' rest of the page
End if
% >
例子2
这里有另外一个简单的例子:当访问者第1次浏览某个站点时,向他们显示欢迎信息。代码如下:
< %@ LANGUAGE = " VBSCRIPT " % >
< %
RequestName = Request.Form( " Name " )
RequestLeaveMeAlone = Request.Form( " LeaveMeAlone " )
If RequestName < > "" or RequestLeaveMeAlone < > "" then
Response.Cookies( " MySiteVisitorName " ) = RequestName
Response.Cookies( " MySiteVisitorName " ).Expires = #January 01 , 2010 #
Response.Cookies( " MySiteLeaveMeAlone " ) = RequestLeaveMeAlone
Response.Cookies( " MySiteLeaveMeAlone " ).Expires = #January 01 , 2010 #
End if
VisitorName = request.cookies( " MySiteVisitorName " )
LeaveMeAlone = request.cookies( " MySiteLeaveMeAlone " )
If VisitorName = "" and LeaveMeAlone = "" then
% >
< HTML > < HEAD > < / HEAD >
< body bgcolor = " #ccffff " text = " black " link = " navy " vlink = " purple " >
< DIV ALIGN = " CENTER " >
< form action = " index.asp " method = " POST " >
< H2 > Let ' s be friends< /H2 >
What ' s your name (leave blank and hit the Submit button if you don't want us to know)?
< input type = " text " name = " name " >< BR >< BR >
< input type = " hidden " name = " LeaveMeAlone " value = " x " >
< input type = " submit " value = " Submit " >
< / FORM >
< / DIV >
< / BODY >
< %
End if
If VisitorName < > "" then
Response.write " Hi, " & VisitorName & " ! I hope you are having a great day! "
End if
' rest of the page
% >
好,现在来看看上面的代码实现执行了什么。首先,设置页面。然后,检查表单变量(在同一个页面中)。如果表单变量存
在,就创建cookie,并设置到期时间。
这里有另外一个简单的例子:当访问者第1次浏览某个站点时,向他们显示欢迎信息。代码如下:
< %@ LANGUAGE = " VBSCRIPT " % >
< %
RequestName = Request.Form( " Name " )
RequestLeaveMeAlone = Request.Form( " LeaveMeAlone " )
If RequestName < > "" or RequestLeaveMeAlone < > "" then
Response.Cookies( " MySiteVisitorName " ) = RequestName
Response.Cookies( " MySiteVisitorName " ).Expires = #January 01 , 2010 #
Response.Cookies( " MySiteLeaveMeAlone " ) = RequestLeaveMeAlone
Response.Cookies( " MySiteLeaveMeAlone " ).Expires = #January 01 , 2010 #
End if
VisitorName = request.cookies( " MySiteVisitorName " )
LeaveMeAlone = request.cookies( " MySiteLeaveMeAlone " )
If VisitorName = "" and LeaveMeAlone = "" then
% >
< HTML > < HEAD > < / HEAD >
< body bgcolor = " #ccffff " text = " black " link = " navy " vlink = " purple " >
< DIV ALIGN = " CENTER " >
< form action = " index.asp " method = " POST " >
< H2 > Let ' s be friends< /H2 >
What ' s your name (leave blank and hit the Submit button if you don't want us to know)?
< input type = " text " name = " name " >< BR >< BR >
< input type = " hidden " name = " LeaveMeAlone " value = " x " >
< input type = " submit " value = " Submit " >
< / FORM >
< / DIV >
< / BODY >
< %
End if
If VisitorName < > "" then
Response.write " Hi, " & VisitorName & " ! I hope you are having a great day! "
End if
' rest of the page
% >
好,现在来看看上面的代码实现执行了什么。首先,设置页面。然后,检查表单变量(在同一个页面中)。如果表单变量存
在,就创建cookie,并设置到期时间。
<
%@ LANGUAGE
=
"
VBSCRIPT
"
%
>
< %
RequestName = Request.Form( " Name " )
RequestLeaveMeAlone = Request.Form( " LeaveMeAlone " )
If RequestName < > "" or RequestLeaveMeAlone < > "" then
Response.Cookies( " MySiteVisitorName " ) = RequestName
Response.Cookies( " MySiteVisitorName " ).Expires = #January 01 , 2010 #
Response.Cookies( " MySiteLeaveMeAlone " ) = RequestLeaveMeAlone
Response.Cookies( " MySiteLeaveMeAlone " ).Expires = #January 01 , 2010 #
End if
接着,读取cookie:
VisitorName = request.cookies( " MySiteVisitorName " )
LeaveMeAlone = request.cookies( " MySiteLeaveMeAlone " )
如果cookie在访问者的计算机上不存在,就创建一个表单,询问相关信息:
If VisitorName = "" and LeaveMeAlone = "" then
% >
< HTML >
< HEAD >
< / HEAD >
< body bgcolor = " #ccffff " text = " black " link = " navy " vlink = " purple " >
< DIV ALIGN = " CENTER " >
< form action = " index.asp " method = " POST " >
< H2 > Let ' s be friends< /H2 >
What ' s your name (leave blank and hit the Submit button if you don't want us to know)?
< input type = " text " name = " name " >< br >< br >
< input type = " hidden " name = " LeaveMeAlone " value = " x " >
< input type = " submit " value = " Submit " >
< / FORM >
< / DIV >
< / BODY >
< %
End if
如果cookie已经存在,并且用户名字存在,就显示给访问者一个欢迎界面,然后执行其余的代码。
< %
RequestName = Request.Form( " Name " )
RequestLeaveMeAlone = Request.Form( " LeaveMeAlone " )
If RequestName < > "" or RequestLeaveMeAlone < > "" then
Response.Cookies( " MySiteVisitorName " ) = RequestName
Response.Cookies( " MySiteVisitorName " ).Expires = #January 01 , 2010 #
Response.Cookies( " MySiteLeaveMeAlone " ) = RequestLeaveMeAlone
Response.Cookies( " MySiteLeaveMeAlone " ).Expires = #January 01 , 2010 #
End if
接着,读取cookie:
VisitorName = request.cookies( " MySiteVisitorName " )
LeaveMeAlone = request.cookies( " MySiteLeaveMeAlone " )
如果cookie在访问者的计算机上不存在,就创建一个表单,询问相关信息:
If VisitorName = "" and LeaveMeAlone = "" then
% >
< HTML >
< HEAD >
< / HEAD >
< body bgcolor = " #ccffff " text = " black " link = " navy " vlink = " purple " >
< DIV ALIGN = " CENTER " >
< form action = " index.asp " method = " POST " >
< H2 > Let ' s be friends< /H2 >
What ' s your name (leave blank and hit the Submit button if you don't want us to know)?
< input type = " text " name = " name " >< br >< br >
< input type = " hidden " name = " LeaveMeAlone " value = " x " >
< input type = " submit " value = " Submit " >
< / FORM >
< / DIV >
< / BODY >
< %
End if
如果cookie已经存在,并且用户名字存在,就显示给访问者一个欢迎界面,然后执行其余的代码。
If
VisitorName
<
>
""
then
Response.write " Hi, " & VisitorName & " ! I hope you are having a great day! "
End if
' rest of the page
% >
尽管上面的这个例子很简单,但可以从中扩展许多富有创造力的应用。你可以在表单中加入许多功能,以便定制化web站点。
你还可以让访问者定制网站的色彩、字体,以至于其他web元素。有可能的话,你可以询问访问者的生日,当访问者在那一天来访
时,你就可以显示“生日快乐”的信息给他。
如你所见,cookie的扩展性是无穷的,这篇文章仅仅是抛砖引玉。
Response.write " Hi, " & VisitorName & " ! I hope you are having a great day! "
End if
' rest of the page
% >
尽管上面的这个例子很简单,但可以从中扩展许多富有创造力的应用。你可以在表单中加入许多功能,以便定制化web站点。
你还可以让访问者定制网站的色彩、字体,以至于其他web元素。有可能的话,你可以询问访问者的生日,当访问者在那一天来访
时,你就可以显示“生日快乐”的信息给他。
如你所见,cookie的扩展性是无穷的,这篇文章仅仅是抛砖引玉。