判断Cookies是否处于开启状态

当开发人员使用Session变量时必须要求客户端浏览器支持接受cookies,当ASP开始一个Session,它将送一个cookies到客户端并带一个标示(SessionID).通过这个标示,ASP以后将可以确认Session并且因此来保持状态。所以,当你使用Session变量前,你需要确认cookies是否被接受。这里我讲解两个方法:

方法一:
无论何时你在两页之间,有一个非常简单可靠的方法:请求一个SessionID在第一页,传递它到下一页。与这一页请求到的SessionID比较。相同说明客户端浏览器接受Cookies;不同则不接受。很简单吧。
比如你可以在第一页中放一个(hidden field),并把SessionID写入它。提交后,从页面数据中取出SessionID.像这样:

1<form action="sessions2.asp" method="post" name="Form1">   
2UserName:<input name="username"/><br/>   
3Password:<input name="userpassword"/>
4<input name="theSessionID" type="hidden" value="```
5=Session.SessionID
6```"/><br/>
7<input type="submit" value="Submit"/>
8</form>

在第二页中我们来判断SessionID是否相同。

 1   
 2dim theSessionID   
 3theSessionID = Request.Form("theSessionID")   
 4If theSessionID = Session.SessionID Then   
 5"当二者相等时,则cookie功能开启   
 6Response.Write "Cookie已开启"   
 7Else   
 8"若二者相等时,则cookie功能关闭   
 9Response.Write "Cookie没有开启!"   
10End If   

方法二:
也可用这种方法,首先在一个页面里写入一个cookie,如:

1   
2Response.Cookies("status")="onoroff"   

在第二页里读出此cookie:

1   
2if Request.Cookies("status")="" then   
3"当cookies("status")里没有值时,则cookie功能没有开启   
4Response.Write "Cookie没有开启!"   
5else   
6"当cookies("status")里有值时,则cookie功能开启   
7Response.Write "Cookie已开启"   
8end if   
Published At
Categories with Web编程
Tagged with
comments powered by Disqus