?? asp中cookie使用指南.htm
字號:
Request.Cookies(Item) %></TD> <BR><TD><% =
Request.Cookies(Item).HasKeys %></TD> <BR></TR>
<BR><TR> <BR><% <BR> If Request.Cookies(Item).HasKeys
Then <BR> For Each strSubKey In Request.Cookies(Item)
<BR>%> <BR><TD>&bnsp;</TD>
<BR><TD>&bnsp;</TD> <BR><TD><% =
Request.Cookies(strKey)(strSubKey) %></TD> <BR><%
<BR>Next <BR>End If <BR>Next <BR>%> <BR></TABLE>
<BR><BR>request.ServerVariables("HTTP_COOKIE"),粒子: <BR><TABLE
BORDER="2"> <BR><THEAD> <BR><TH>Cookie
Name</TH> <BR><TH>Cookie Value</TH>
<BR></THEAD> <BR><% <BR>Dim Item,sp,i,d <BR>sp =
split(request.ServerVariables("HTTP_COOKIE"),"; ",-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)),"=",-1,1)
<BR>%> <BR><TR> <BR><TD><% = d(0) %></TD>
<BR><TD><% if UBound(d)=1 then Response.Write(d(1)) else
Response.Write " " %></TD> <BR></TR> <BR><%
<BR>Next <BR>%> <BR></TABLE>
<BR><BR>附:甘冀平翻譯的《ASP中Cookie使用指南》 <BR><BR>
實際上,在web開發中,cookie僅僅是一個文本文件,當用戶訪問站點時,它就被存儲在用戶使用的計算機上,其中,保存了一些信息,當用戶日后再次訪問這個站點時,web可以將這些信息提取出來。
<BR><BR>
盡管現在聽起來cookie沒有什么激動人心的,但實際上利用它,你能實現許多有意義的功能!比如說:你可以在站點上放置一個調查問答表,詢問訪問者最喜歡的顏色和字體,然后根據這些定制用戶的web界面。并且,你還可以保存訪問者的登錄密碼,這樣,當訪問者再次訪問這個站點時,不用再輸入密碼進行登錄。
<BR><BR>
當然,cookie也有一些不足。首先,由于利用cookie的功能可以編程實現一些不良企圖,所以大多數的瀏覽器中都有安全設定,其中可以設置是否允許或者接受cookie,因此這就不能保證隨時能使用cookie。再者,訪問者可能有意或者無意地刪除cookie。當訪問者的機器遇到“藍屏”死機時,或者重新格式化硬盤、安裝系統后,原來保存的cookie將全部丟失。最后一點,有一些最初始的瀏覽器并不能支持cookie。
<BR><BR> 利用cookie能做什么? <BR><BR> 有2種使用cookie的基本方式:
<BR><BR>1、將cookie寫入訪問者的計算機(使用 RESPONSE 命令)
<BR>2、從訪問者的計算機中取回cookie(使用 REQUEST 命令) <BR><BR> 創建cookie的基本語法
<BR><BR> Response.Cookies("CookieName")=value <BR><BR>
執行下面的代碼將會在訪問者的計算機中創建一個cookie,名字=VisitorName,值=Ken
<BR>Response.Cookies("VisitorName")="Ken" <BR><BR>
執行下面的代碼將會在訪問者的計算機中創建一個cookie,名字=VisitorName,值=表單中UserName的值
<BR>Response.Cookies("VisitorName")=Request.Form("UserName")
<BR><BR> 讀取cookie的基本語法 <BR><BR> Request.Cookies("CookieName")
<BR><BR>
可以將Request值當作一個變量看待,執行下面的代碼,將取回名字為KensCookie的cookie值,并存入變量MyVar:
<BR>MyVar=Request.Cookies("KensCookie") <BR><BR>
執行下面的代碼,將判斷名字為KensCookie的cookie值是否為“Yes”: <BR>If
Request.Cookies("KensCookie")="Yes" then <BR><BR> 功能豐富的cookie
<BR><BR> 你可以擴展上面的代碼成為Cookie子關鍵值(CookieSubName),代碼如下:
<BR>Response.Cookies("VisitorName")("FirstName")="Ken"
<BR>Response.Cookies("VisitorName")("LastName")="Baumbach"
<BR><BR> 講解例子前,最后討論2個概念:命令約定和使用到期時間。 <BR><BR> 命名約定 <BR><BR>
同其他變量的命名一樣,合適地、獨特地命名cookie,有利于在程序中前后連貫地使用它。你可以使用下面的1個或者2個cookie屬性進行cookie變量的命名:
<BR><BR>
域屬性(Domain):域屬性表明cookie由哪個網站產生或者讀取,默認情況下,cookie的域屬性設置為產生它的網站,但你也可以根據需要改變它。相關代碼如下:Response.Cookies("CookieName").Domain
= "www.mydomain.com" <BR><BR>
路徑屬性(Path):路徑屬性可以實現更多的安全要求,通過設置網站上精確的路徑,就能限制cookie的使用范圍。例如:
<BR>Response.Cookies("CookieName").Path = "/maindir/subdir/path"
<BR><BR> 使用到期時間 <BR><BR>
通常情況下,當瀏覽器關閉時,一個cookie就不存在了。但是在許多時候,比如下面將要討論的web站點例子,我們希望能更長時間地在訪問者的計算機上保存cookie。很幸運,有這樣的實現方法。下面的代碼,就可以設置cookie的使用到期時間為2010年1月1日:
<BR>Response.Cookies("CookieName").Expires=#January 01, 2010#
<BR><BR> 執行下面的代碼,將設定cookie的過期時間為“cookie的創建時間+365日”:
<BR>Response.Cookies("CookieName")=Date+365 <BR><BR>
使用cookie的實際例子(非常精彩) <BR><BR>
現在開始討論實際的例子。假設:你想做一個調查,每個人初次訪問時需要填寫好信息,但是當日后再訪問時,就不需要再那么做。利用cookie,就可以非常圓滿地解決這個問題,而大可不必用到數據庫。
<BR><BR>< %@ LANGUAGE="VBSCRIPT" % > <BR>< %
<BR>Survey=Request.Cookies("KensSurvey") <BR>If Survey ="" then
<BR> Response.Cookies("KensSurvey")="x"
<BR> Response.Cookies("KensSurvey").Expires=#January 01, 2010#
<BR> Response.Redirect "survey.asp" <BR>Else <BR>'rest of the
page <BR>End if <BR>% > <BR> 好,下面開始從頭討論上面的代碼。 <BR><BR>
首先,初始設置頁面,并讀取名字為KensSurvey的cookie值: <BR><BR><BR>< %@
LANGUAGE="VBSCRIPT" % > <BR>< %
<BR>Survey=Request.Cookies("KensSurvey") <BR> 然后,判斷是否已經存在cookie值:
<BR><BR>If Survey ="" then <BR> 如果不存在,
就創建并設置cookie,并轉到頁面survey.asp。 當下一次訪問時,因為存在cookie值,就不會再轉到survey.asp
頁面。 <BR><BR> Response.Cookies("KensSurvey")="x" <BR>
Response.Cookies("KensSurvey").Expires=#January 01, 2010# <BR>
Response.Redirect "survey.asp" <BR> 如果cookie已經存在,那么訪問者將執行頁面中剩余的代碼:
<BR><BR>'rest of the page <BR><BR>End if <BR>% > <BR> 例子2
<BR><BR> 這里有另外一個簡單的例子:當訪問者第1次瀏覽某個站點時,向他們顯示歡迎信息。代碼如下: <BR><BR><
%@ LANGUAGE="VBSCRIPT" % > <BR>< % <BR>RequestName =
Request.Form("Name") <BR>RequestLeaveMeAlone =
Request.Form("LeaveMeAlone") <BR>If RequestName < >"" or
RequestLeaveMeAlone < >"" then
<BR> Response.Cookies("MySiteVisitorName") = RequestName
<BR> Response.Cookies("MySiteVisitorName").Expires = #January
01, 2010# <BR> Response.Cookies("MySiteLeaveMeAlone") =
RequestLeaveMeAlone
<BR> Response.Cookies("MySiteLeaveMeAlone").Expires = #January
01, 2010# <BR>End if <BR>VisitorName =
request.cookies("MySiteVisitorName") <BR>LeaveMeAlone =
request.cookies("MySiteLeaveMeAlone") <BR><BR>If VisitorName ="" and
LeaveMeAlone ="" then <BR>% > <BR> < HTML > < HEAD
> < /HEAD > <BR> < body bgcolor="#ccffff"
text="black" link="navy" vlink="purple" > <BR> < DIV
ALIGN="CENTER" > <BR> < form action="index.asp"
method="POST" > <BR> < H2 >Let's be friends< /H2
> <BR> What's your name (leave blank and hit the Submit
button if you don't want us to know)? <BR> < input
type="text" name="name" >< BR >< BR > <BR> <
input type="hidden" name="LeaveMeAlone" value="x" >
<BR> < input type="submit" value="Submit" >
<BR> < /FORM > <BR> < /DIV > <BR> <
/BODY > <BR>< % <BR>End if <BR>If VisitorName < >
"" then <BR> Response.write "Hi, " & VisitorName & "!
I hope you are having a great day!" <BR>End if <BR>'rest of
the page <BR>% > <BR>
好,現在來看看上面的代碼實現執行了什么。首先,設置頁面。然后,檢查表單變量(在同一個頁面中)。如果表單變量存在,就創建cookie,并設置到期時間。
<BR><BR>< %@ LANGUAGE="VBSCRIPT" % > <BR>< %
<BR>RequestName = Request.Form("Name") <BR>RequestLeaveMeAlone =
Request.Form("LeaveMeAlone") <BR>If RequestName < >"" or
RequestLeaveMeAlone < >"" then
<BR> Response.Cookies("MySiteVisitorName") = RequestName
<BR> Response.Cookies("MySiteVisitorName").Expires = #January
01, 2010# <BR> Response.Cookies("MySiteLeaveMeAlone") =
RequestLeaveMeAlone
<BR> Response.Cookies("MySiteLeaveMeAlone").Expires = #January
01, 2010# <BR>End if <BR><BR> 接著,讀取cookie:
<BR><BR>VisitorName = request.cookies("MySiteVisitorName")
<BR>LeaveMeAlone = request.cookies("MySiteLeaveMeAlone") <BR>
如果cookie在訪問者的計算機上不存在,就創建一個表單,詢問相關信息: <BR><BR>If VisitorName ="" and
LeaveMeAlone ="" then <BR>% > <BR> < HTML >
<BR> < HEAD > <BR> < /HEAD > <BR> <
body bgcolor="#ccffff" text="black" link="navy" vlink="purple" >
<BR> < DIV ALIGN="CENTER" > <BR> < form
action="index.asp" method="POST" > <BR> < H2 >Let's be
friends< /H2 > <BR> What's your name (leave blank and hit
the Submit button if you don't want us to know)? <BR> <
input type="text" name="name" >< br >< br >
<BR> < input type="hidden" name="LeaveMeAlone" value="x"
> <BR> < input type="submit" value="Submit" >
<BR> < /FORM > <BR> < /DIV > <BR> <
/BODY > <BR>< % <BR>End if <BR>
如果cookie已經存在,并且用戶名字存在,就顯示給訪問者一個歡迎界面,然后執行其余的代碼。 <BR><BR>If
VisitorName < > "" then <BR> Response.write "Hi, "
& VisitorName & "! I hope you are having a great day!"
<BR>End if <BR>'rest of the page <BR>% > <BR><BR>
盡管上面的這個例子很簡單,但可以從中擴展許多富有創造力的應用。你可以在表單中加入許多功能,以便定制化web站點。你還可以讓訪問者定制網站的色彩、字體,以至于其他web元素。有可能的話,你可以詢問訪問者的生日,當訪問者在那一天來訪時,你就可以顯示“生日快樂”的信息給他。
<BR><BR> 如你所見,cookie的擴展性是無窮的,這篇文章僅僅是拋磚引玉。 <BR></TD></TR>
<TR>
<TD width="100%">
<DIV align=right><INPUT name=button onclick="document.location.href='javascript:sendmail(568)'" type=button value=推薦給朋友>
<INPUT name=button onclick=javascript:window.print() type=button value=打印本頁>
<INPUT name=button onclick=window.close() type=button value=關閉窗口></DIV></TD></TR>
<TR>
<TD width="100%">
<HR noShade SIZE=1>
</TD></TR>
<TR>
<TD class=review width="100%">在此發表你對本文的意見和看法(800字以內) <A
href="javascript:document.review.submit()">發表</A></TD></TR>
<TR>
<TD class=review width="100%"><IMG height=5
src="ASP中Cookie使用指南 - 者網摘.files/space.gif" width=1></TD></TR>
<TR>
<FORM action=review.cgi name=review>
<TD class=review width=590><TEXTAREA cols=70 name=text rows=5></TEXTAREA> <INPUT
name=action type=hidden value=new> <INPUT name=id type=hidden
value=568> </TD></FORM></TR>
<TR>
<TD class=review width="100%"><IMG height=5
src="ASP中Cookie使用指南 - 者網摘.files/space.gif" width=1></TD></TR>
<TR>
<TD class=review width="100%">還沒有其他評論 ...</TD></TR>
<TR>
<TD width="100%">
<HR noShade SIZE=1>
</TD></TR>
<TR>
<TD width="100%">
<TABLE bgColor=#000000 border=0 cellPadding=0 cellSpacing=1
width="100%">
<TBODY>
<TR>
<TD bgColor=#fdc903 height=22 width="100%"> 相關文章</TD></TR>
<TR>
<TD bgColor=#ffffff width="100%">
<UL>
<LI>未找到相關文章 </LI></UL></TD></TR></TBODY></TABLE></TD></TR>
<TR>
<TD width="100%"></TD></TR></TBODY></TABLE></TD>
<TD height=%100 width=11>
<TABLE border=0 cellPadding=0 cellSpacing=0 height="100%" width="100%">
<TBODY>
<TR>
<TD width=5><IMG height=2 src="ASP中Cookie使用指南 - 者網摘.files/space.gif"
width=5></TD>
<TD width=1><IMG height=2 src="ASP中Cookie使用指南 - 者網摘.files/space.gif"
width=1></TD>
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -