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