网络编程
位置:首页>> 网络编程>> Asp编程>> ASP Cookies操作的详细介绍与实例代码(2)

ASP Cookies操作的详细介绍与实例代码(2)

 来源:asp之家 发布时间:2011-03-10 10:53:00 

标签:ASP,Cookies,操作
 

5、Cookie字典
  有时在一个页面中可能需要定义很多个Cookies变量,为了更好地管理它,在Cookies组件中常引入一人的概念“子键”。引用它的语法如下:
  

Request.Cookies("变更名")("子键名")  
  如下面的Cookie创建一个名为"Dictionary"的字典,其中保存了三个键值:

代码如下:


<% 
Response.Cookie("info")("Myname")="jeff" 
Response.Cookie("info")("Gender")="male" 
Response.Cookie("info")("Myheight")="172" 
%> 


  事实上客户机上的Cookie字典是以字符串的形式存在:
info=Myname=jeff&Gender=male&Myheight=172
  如果用户没有指定“子键”名而直接引用Cookies变量,将会返回一个包含所有的“子键”名及值的字符串。例如上面这个例子包含三个“子键”:"Myname"、"Gender"和"Myheight",当用户没有指定其“子键”而直接通过Request.Cookies("info")来引用时,则会得到下列字符串:
info=Myname=jeff&Gender=male&Myheight=172
  如果要把Cookie中读取的所有数据,可以用下面的代码得到:

代码如下:


<%For each cookie in Request.Cookies 
if Not cookie.HasKeys then 
Response.write cookie & "=" & Request.Cookies(cookie) 
Else 
for each key in Request.Cookies(cookie) 
Response.write cookie&"("&key&")"&"="& Request.Cookies(cookie)(key) 
next 
end if 
next 
%> 

下面是具体的在页面中记录查询的记录的代码

代码如下:


Sub SetCookie 
Dim C_DomainList,C_i 
C_DomainList=Request.Cookies("jb51")("C_DomainList") 
If Domain<>"" and C_DomainList<>"" then 
If not instr(C_DomainList,Domain&"|")>0 then C_DomainList=Domain&"|"&C_DomainList 
End if 
If Domain<>"" and C_DomainList="" then 
C_DomainList=Domain&"|" 
End if 
If C_DomainList<>"" then 
Response.write "<div id=C_domainlist>您关注的站点:" 
C_arrDomain = split(C_DomainList,"|") 
C_DomainList="" 
numDomain=ubound(C_arrDomain)-1 
If numDomain>4 then numDomain=4 
for C_i=0 to numDomain 
Response.write " <a href=?url="&C_arrDomain(C_i)&">"&C_arrDomain(C_i)&"</a> |" 
C_DomainList=C_DomainList&C_arrDomain(C_i)&"|" 
next 
Response.Cookies("jb51")("C_DomainList")=C_DomainList 
Response.Cookies("jb51").Expires=Date+30 
Response.write "<a href=# style=""cursor:pointer"" onClick=""clearCookie('jb51');alert('已清除记录!');"">清除记录</a></div>" 
End If 
End Sub


0
投稿

猜你喜欢

手机版 网络编程 asp之家 www.aspxhome.com