[转]ASP中Cookie使用指南

我自己曾想写一篇关于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)
=   split ( cstr (sp(i)), " = " , - 1 , 1 )
%
>
< TR >
< TD >< =  d( 0 ) % ></ TD >
< TD >< if   UBound (d) = 1   then  Response.Write(d( 1 ))  else  Response.Write  " &nbsp; "  % ></ 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 " )

读取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 #

  执行下面的代码,将设定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
>
   好,下面开始从头讨论上面的代码。
  首先,初始设置页面,并读取名字为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
>
  例子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,并设置到期时间。
<  %@ 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已经存在,并且用户名字存在,就显示给访问者一个欢迎界面,然后执行其余的代码。

 

If  VisitorName  <    >   ""   then
  Response.write 
" Hi,  "   &  VisitorName  &   " !  I hope you are having a great day! "
End   if
' rest of the page
>
   尽管上面的这个例子很简单,但可以从中扩展许多富有创造力的应用。你可以在表单中加入许多功能,以便定制化web站点。
你还可以让访问者定制网站的色彩、字体,以至于其他web元素。有可能的话,你可以询问访问者的生日,当访问者在那一天来访
时,你就可以显示“生日快乐”的信息给他。

   如你所见,cookie的扩展性是无穷的,这篇文章仅仅是抛砖引玉。

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
/* 豆腐制作 都是精品 www.asp888.net 豆腐技术站 如果载 请添加这个 版权信息 */ [removed] Const COOKIE_NAME As String = "豆腐站点" Const COOKIE_VALUE As String = "http://www.asp888.net" ' 声明 Cookie 对象 Dim objCookieObject As HttpCookie Sub btnSetCookie_OnClick(Sender As Object, E As EventArgs) ' Create a cookie object - I'm passing name and value, ' but you can also pass in a name and set the value later. objCookieObject = New HttpCookie(COOKIE_NAME, COOKIE_VALUE) ' 另外的一种操作Cookie 的 方法 'objCookieObject = New HttpCookie(COOKIE_NAME) 'objCookieObject.Name = COOKIE_NAME 'objCookieObject.Value = "sdsd" ' 设置Cookie 的 过期时间 2001/12/31 23:59:59 objCookieObject.Expires = New DateTime(2001, 12, 11, 23, 59, 59) ' 下面的这些可以不使用 objCookieObject.Domain = "www.asp888.net" objCookieObject.Path = "/path/" objCookieObject.Secure = True Response.AppendCookie(objCookieObject) End Sub Sub btnRemoveCookie_OnClick(Sender As Object, E As EventArgs) objCookieObject = New HttpCookie(COOKIE_NAME) ' 删除Cookie objCookieObject.Expires = New DateTime(1974, 11, 12) Response.AppendCookie(objCookieObject) End Sub Sub btnGetCookie_OnClick(Sender As Object, E As EventArgs) objCookieObject = Request.Cookies(COOKIE_NAME) If Not(objCookieObject = null) Then lblCookieDetails.Text = objCookieObject.Name lblCookieDetailsName.Text = objCookieObject.Name lblCookieDetailsValue.Text = objCookieObject.Value lblCookieDetailsExpires.Text = objCookieObject.Expires.ToString lblCookieDetailsDomain.Text = objCookieObject.Domain lblCookieDetailsPath.Text = objCookieObject.Path lblCookieDetailsSecure.Text = objCookieObject.Secure.ToString lblCookieDetailsHasKeys.Text = objCookieObject.HasKeys.ToString Else lblCookieDetails.Text = "Cookie Not Set!" lblCookieDetailsName.Text = "" lblCookieDetailsValue.Text = "" lblCookieDetailsExpires.Text = "" lblCookieDetailsDomain.Text = "" lblCookieDetailsPath.Text = "" lblCookieDetailsSecure.Text = "" lblCookieDetailsHasKeys.Text = "" End If End Sub [removed] <html> <head>

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值