⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 0050.htm

📁 ASP教程宝典 书籍语言: 简体中文 书籍类型: 网络编程 授权方式: 免费软件 书籍大小: 500 KB
💻 HTM
字号:
<html>

<head>
<title>新时代软件教程:操作系统 主页制作 服务器 设计软件 网络技术 编程语言 文字编辑</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<style>
<!--
body, table {font-size: 9pt; font-family: 宋体}
a {text-decoration:none}
a:hover {color: red;text-decoration:underline}
.1  {background-color: rgb(245,245,245)}
-->
</style>
</head>
<p align="center"><script src="../../1.js"></script></a>
    <p align="center"><big><strong>ASP中Cookie使用指南</strong></big></p>

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

  </table>
<p align="center"><script src="../../2.js"></script></a>
</body>
</html>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -