<script type="text/javascript"><!--
google_ad_client = "pub-6201975834087621";
google_ad_width = 300;
google_ad_height = 250;
google_ad_format = "300x250_as";
google_ad_type = "text_image";
google_ad_channel ="";
google_color_border = "FFFFFF";
google_color_bg = "FFFFFF";
google_color_link = "000000";
google_color_url = "CC6600";
google_color_text = "000000";
//--></script>
<script src="http://pagead2.googlesyndication.com/pagead/show_ads.js" type="text/javascript">
</script>
<script type="text/javascript">
<!--
function log() {
if (window.status.indexOf('go to') == 0) {
bug = new Image();
bug.src = '/Google/c.asp?uid=100009&L=' + document.location + '&G=' + window.status.substring(6);
}
if (window.status.indexOf('鍵連至') == 0) {
bug = new Image();
bug.src = '/Google/c.asp?uid=100009&L=' + document.location + '&G=' + window.status.substring(3);
}
}
var elements;
elements = document.getElementsByTagName("iframe");
for (var i = 0; i < elements.length; i++) {
if(elements[i].src.indexOf('googlesyndication.com') > -1) {
elements[i].onfocus = log;
}
}
//-->
</script>
|
實際上,在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
'rest of the page
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已經存在,那麼訪問者將執行頁面中剩餘的代碼:
'rest of the page
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="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
'rest of the page
% >
好,現在來看看上面的代碼實現執行了什麼。首先,設置頁面。然後,檢查表單變量(在同一個頁面中)。如果表單變量存在,就創建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
'rest of the page
% >
儘管上面的這個例子很簡單,但可以從中擴展許多富有創造力的應用。你可以在表單中加入許多功能,以便定製化web站點。
你還可以讓訪問者定製網站的色彩、字體,以至於其他web元素。有可能的話,你可以詢問訪問者的生日,當訪問者在那一天來訪
時,你就可以顯示“生日快樂”的信息給他。
如你所見,cookie的擴展性是無窮的,這篇文章僅僅是拋磚引玉。