ASP设置Cookies应用指南

实际上,在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
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已经存在,那么访问者将执行页面中剩余的代码:
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='./../../&'quot;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
%>

好,现在来看看上面的代码实现执行了什么。首先,设置页面。然后,检查表单变量(在同一个页面中)。如果表单变量存在,就创建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  
%>

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 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、付费专栏及课程。

余额充值