实际上,在web开发中,cookie仅仅是一个文本文件,当用户访问站点时,它就被存储在用户使用的计算机上,其中,保存了一些信息,当用户日后再次访问这个站点时,web可以将这些信息提取出来。 尽管现在听起来cookie没有什么激动人心的,但实际上利用它,你能实现许多有意义的功能!比如说:你可以在站点上放置 当然,cookie也有一些不足。首先,由于利用cookie的功能可以编程实现一些不良企图,所以大多数的浏览器中都有安全设定,其中可以设置是否允许或者接受cookie,因此这就不能保证随时能使用cookie。再者,访问者可能有意或者无意地删除cookie。当访问者的机器遇到“蓝屏”死机时,或者重新格式化硬盘、安装系统后,原来保存的cookie将全部丢失。最后一点,有一些最初始的浏览器并不能支持cookie。 利用cooklie能做什么? 有2种使用cookie的基本方式: 创建cookie的基本语法
功能丰富的cookie 你可以扩展上面的代码成为Cookie子关键值(CookieSubName),代码如下:
同其他变量的命名一样,合适地、独特地命名cookie,有利于在程序中前后连贯地使用它。你可以使用下面的1个或者2个 域属性(Domain):域属性表明cookie由哪个网站产生或者读取,默认情况下,cookie的域属性设置为产生它的网站,但你 路径属性(Path):路径属性可以实现更多的安全要求,通过设置网站上精确的路径,就能限制cookie的使用范围。例如: 使用到期时间 通常情况下,当浏览器关闭时,一个cookie就不存在了。但是在许多时候,比如下面将要讨论的web站点例子,我们希望能更长时间地在访问者的计算机上保存cookie。很幸运,有这样的实现方法。下面的代码,就可以设置cookie的使用到期时间为2010年1月1日: Response.Cookies("CookieName").Expires=#January 01, 2010# 执行下面的代码,将设定cookie的过期时间为“cookie的创建时间+365日”: 现在开始讨论实际的例子。假设:你想做一个调查,每个人初次访问时需要填写好信息,但是当日后再访问时,就不需要再那么做。利用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,并转到页面survey.asp。 当下一次访问时,因为存在cookie值,就不会再转到survey.asp 页面。
Response.Cookies("KensSurvey")="x"
'rest of the page End if
< %@ 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 % >
< %@ 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")
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 End if 'rest of the page % >
|
ASP:Cookie使用指南
最新推荐文章于 2023-09-05 13:03:53 发布
ASP:Cookie使用指南