|
||
Cookie是一種在客戶端保持HTTP狀態(tài)信息的技術,它好比商場發(fā)放的優(yōu)惠卡。顧客在一個商場購物結賬離開時,商場可以決定是否贈送給顧客一張優(yōu)惠卡,不同顧客的優(yōu)惠卡上記載的信息可以不同,例如,記載該顧客累計購物的金額和有效期限。顧客可以決定是否接受這張優(yōu)惠卡,一旦顧客接受了這張優(yōu)惠卡,那么他在以后每次光顧該商場時,都將攜帶這張優(yōu)惠卡,商場也將根據(jù)這張優(yōu)惠卡上記載的信息進行一些特殊的事務處理,例如,計算折扣率和累加本次購物金額。Cookie是在瀏覽器訪問WEB服務器的某個資源時,由WEB服務器在HTTP響應消息頭中附帶傳送給瀏覽器的一片數(shù)據(jù),WEB服務器傳送給各個客戶端瀏覽器的數(shù)據(jù)是可以各不相同的。瀏覽器可以決定是否保存這片數(shù)據(jù),一旦WEB瀏覽器保存了這片數(shù)據(jù),那么它在以后每次訪問該WEB服務器時,都應在HTTP請求頭中將這片數(shù)據(jù)回傳給WEB服務器。顯然,Cookie最先是由WEB服務器發(fā)出的,是否發(fā)送Cookie和發(fā)送的Cookie的具體內容,完全是由WEB服務器決定的。 WEB服務器通過在HTTP響應消息中增加Set-Cookie響應頭字段將Cookie信息發(fā)送給瀏覽器,瀏覽器則通過在HTTP請求消息中增加Cookie請求頭字段將Cookie回傳給WEB服務器。一個Cookie只能標識一種信息,它至少含有一個標識該信息的名稱(NAME)和設置值(value)。一個WEB站點可以給一個WEB瀏覽器發(fā)送多個Cookie,這樣,在WEB瀏覽器和WEB服務器之間就可以使用多個Cookie來傳遞多種信息,例如,用一個Cookie來標識訪問者的姓名,用另外一個Cookie來標識該用戶登錄站點的次數(shù)。一個Cookie除了有名稱和設置值外,它還可以有一些其他的附加屬性,例如,Cookie的有效時間。如果設置了Cookie的有效時間,接受它的瀏覽器進程將該Cookie保存在計算機硬盤中,只有該Cookie超出有效時間后才被刪除,這樣的Cookie將被同一臺計算機上啟動的多個瀏覽器進程共享。正如一個顧客可以有多家商場提供的優(yōu)惠卡一樣,一個WEB瀏覽器也可以存儲多個WEB站點提供的Cookie。為了防止Cookie塞滿客戶機的硬盤,瀏覽器一般只允許存放300個Cookie,每個站點最多存放20個Cookie,每個Cookie的大小限制為4KB。如果沒有設置Cookie的有效時間,接受它的瀏覽器進程只將該Cookie保存在自己的內存空間中,在該瀏覽器進程關閉時,它里面保存的所有Cookie也將隨之消失。 Cookie實現(xiàn)了一種在瀏覽器和服務器之間產生有狀態(tài)會話的方式,它可以把一個瀏覽器訪問的同一個服務器上的所有程序貫連起來,在這些程序之間傳遞數(shù)據(jù)。例如,當用戶使用瀏覽器訪問某個網站的登錄程序進行登錄后,無論這個瀏覽器再訪問該網站的哪個程序,其他程序都能知道訪問者的身份信息,這是在WEB站點中非常普遍的一個應用。這種應用通常就是采用Cookie技術來實現(xiàn)的,當WEB服務器程序驗證登錄請求中的用戶名和密碼后,產生一個標識該用戶身份的標識號,然后在響應消息中將該標識號以Cookie的形式傳遞給瀏覽器,瀏覽器在以后每次訪問該WEB服務器時,都自動在請求消息頭中將標識號又以Cookie的形式返回給WEB服務器,憑借瀏覽器返回的標識號,WEB服務器的其他程序就能分辨出當前請求是由哪個用戶發(fā)出的。但是,有一點要注意,不保存在硬盤中的Cookie信息是否可以被同一臺計算機上啟動的多個瀏覽器進程共享,不同的瀏覽器有不同的處理方式。對于IE瀏覽器來說,保存在其中一個瀏覽器進程的內存空間中的Cookie是不能被其他瀏覽器進程共享的,這就會出現(xiàn)同一臺計算機上的每個瀏覽器進程都會與服務器形成各自獨立的會話;而對于Mozilla ?Firefox瀏覽器來說,所有的進程和標簽頁都共享cookie信息。另外,在IE瀏覽器中按Ctrl-N鍵(或者單擊“文件”?;;“新建”?;;“窗口”菜單)打開的窗口或者是用javascript的window.open語句打開的窗口,都會共享原窗口的Cookie信息,因為它們屬于同一個瀏覽器進程內部的多個窗口(出自張孝祥老師的<深入體驗JavaWeb開發(fā)內幕>手稿很期待這本書的面世) /// 我們學習了可以用jsp Cookie類來創(chuàng)建cookie,當然其它語言也可以創(chuàng)建包括客戶端腳本語言javascript,vbscript同在我們主要談談用javascript,和html來創(chuàng)建cookie 1 Creating a Cookie that Is Valid Until a Certain Date HTTP-EQUIV="Set-Cookie" CONTENT="userId=678;expires=Wednesday, 26-Dec-01 16:00:00 GMT; path=/"> Unless you set your browser to not accept cookies, a cookie called userId with a value of 678 has been created for you. ///// 2 Creating Cookies with document.cookiess document.cookiess = "cookieName=cookievalue [; expires=timeInGMTString] [; path=pathName] [; domain=domainName] [; secure]" Listing 25.3 Creating a Cookie with document.cookiess This page creates a cookie on the client side. Make sure that your browser is set to accept cookies. Creating Cookies with the setCookie Function For example, you‘ll want to create a cookie when your user chooses to buy something in your online store web application Listing 25.4 The setCookie Function Listing 25.7 An Example that Creates a Cookie with an Expiration Date A cookie which is valid for a year has been created for this page. Listing 25.9 Writing and Reading Cookies Type in your user id, and then click the Create Cookie button. A cookie will be created for you. User ID: onClick=‘setCookie("UserID", document.FORMs[0].UserID.value)‘> Click the Read Cookie button to display the cookie. onClick=‘alert(getCookie("UserID"))‘> Listing 25.10 Deleting a Cookie Checking If the Browser Can Accept Cookies Using javascript 用javascript來檢查用戶瀏覽器是否支持cookie技術 Listing 25.11 Checking If the Browser Can Accept Cookies Using javascript The page content Checking If the Browser Accepts Cookies Without javascript 不用javascript來檢查用戶瀏覽器是否支持cookie技術!??! Another way to check if the browser is willing to accept cookies is by creating a cookie on one page and then immediately redirecting the user to a second page. In the second page you can then try to read the cookies. The code in Listing 25.12 uses the tag to create a cookie called "test" and then redirects the browser to a second page called checkCookie.jsp (in Listing 25.13). Listing 25.12 Checking Browser Cookie Acceptance with Redirection In the second page, implemented using ASP in this example, you try to read the same cookie using the code in Listing 25.13. Listing 25.13 Reading the Cookies in the Browser Cookie Acceptance Test <% If Request.cookiesss("test") <> "" Then Response.Write "Cookies accepted." Else Response.Write "Cookies not accepted." End If %> Even though the code in this example only sends a message to the user telling him or her whether or not his or her browser accepts cookies, you can modify it to suit your needs. For instance, you can transfer the user to a warning page if the cookies are not accepted. 了解一個cookie文件里面的內容的含義!?。。。。?! 3.5 What are all those entries in my cookies.txt file? The layout of Netscape‘s cookies.txt file is such that each line contains one name-value pair. An example cookies.txt file may have an entry that looks like this: .netscape.com ? ? TRUE ? / ?FALSE ?946684799 ? NETSCAPE_ID ?100103 Each line represents a single piece of stored inFORMation. A tab is inserted between each of the fields. From left-to-right, here is what each field represents: domain - The domain that created AND that can read the variable. flag - A TRUE/FALSE value indicating if all machines within a given domain can access the variable. This value is set automatically by the browser, depending on the value you set for domain. path - The path within the domain that the variable is valid for. secure - A TRUE/FALSE value indicating if a secure connection with the domain is needed to access the variable. expiration - The UNIX time that the variable will expire on. UNIX time is defined as the number of seconds since Jan 1, 1970 00:00:00 GMT. name - The name of the variable. value - The value of the variable. 好現(xiàn)在還看看其它方面的,有趣的一面! 在window當中cookie一般存放在C:\Documents and Settings\wwwfox\Cookies wwwfox為我登錄xp的賬號,當然由于ie版本不同存放的位置也有所不同,你可以到微軟官方網站上去查看,當然最好的方法就是搜一下Cookie文件就可以知道具體在存放在那里了, 下面是Cookies下面的一個Cookie文件wwwfox@cgi-bin.txt內容是: advpost 0 219.239.245.203/cgi-bin/ 1536 1276665728 29789832 2326352352 29783688 對應方法 advpost--->cookie .getName 0--->cookie .getvalue() 1536-->cookie .getMaxAge()如返回為-1表示關閉瀏覽器cookie就失效 后面就是創(chuàng)建日期,失效日期 創(chuàng)建時間,失效時間 現(xiàn)在教你怎么用jsp編程查看上網時網站給你創(chuàng)建的cookie文件, 問題一.cookiess只能由它創(chuàng)建的網站訪問,那么我們怎么能夠在自己的電腦是運行自己的服務器查看出來呢,如果能成那就是cookie欺騙。侵入別人網站的根源。 好通過示例來演示:, wwwfox@csdn[2].txt當然它的命名是 你的用戶電腦賬號@產生的COOKIE的網頁文件所在的WEB目錄[COOKIE改變的次數(shù)].txt wwwfox電腦賬號在訪問csdn網站時是不會發(fā)送到csdn網站上去的,也就是只能在我的電腦wwwfox賬號上使用這個cookie其人(賬號)是不能用的。。 在C:\Documents and Settings\wwwfox\Cookies下我有一個登錄csdn網站時由它創(chuàng)建的cookie wwwfox@csdn[2].txt的內容是: ABCDEF hbWhkUhCWfRTUyeAVO5k79qTBFZXtGV2qfwAROv%252fvVk9qF3rfkcZQbl1IdMsBxhy15Y%252fqwN2XiLyTy%252fDGuW4LY7zZll16huuVbnJ0CEun26I%252f2bynXLPd2Ymq%252bn2Mt11pSP5w3%252fZNXt9ZJEp79VMCw%253d%253d csdn.net/ 1536 3743055744 29786511 2555961280 29783695 //////////////////////////// 在C:\Program Files\Apache Software Foundation\Tomcat 5.5\webapps\test新建一個文件checkCookie.jsp <%@page pageEncoding="gb2312" %> <% Cookie[] cookie=request.getCookies(); out.println("this is www.csdn.net/ "); out.println(" "); for(int i=0;i { if(cookie .getName().equals("ABCDEF")) { String name=cookie .getName(); String value=cookie .getvalue(); int maxage=cookie .getMaxAge(); out.println("name="+cookie .getName()+" "); out.println("value="+cookie .getvalue()+" "); out.println("maxage="+cookie .getMaxAge()+" "); //out.println("setMaxAge為30"); //cookie .setMaxAge(30); //out.println("之后為maxage="+cookie .getMaxAge()+" "); out.println("domain="+cookie .getDomain()+" "); out.println("secure="+cookie .getSecure()+" "); out.println("path="+cookie .getPath()+" "); } } %> 好現(xiàn)在訪問一下 http://localhost:8080/test/checkCookie.jsp是不是顯示: this is www.csdn.net 而沒有取到值:因為只能在它的domain下能訪問到這個cookie 好現(xiàn)在我們來 把C:\WINDOWS\SYSTEM32\DRIVERS\etc下面的hosts.sam打開修改其中的 127.0.0.1 ?hostname 為127.0.0.1 ? ? ? www.csdn.net 在這里你還可以再添加多行和上面相訪的鍵值對,這個比外網的dsn優(yōu)先級高所以它先把www.csdn.net解釋成本地的127.0.0.1所以你就可以由這個訪問了,這也可以解釋為什么我們可以在ie敲http://localhsot的原因,這里localhost你可隨便改成你喜歡的名稱. 保存 修改conf/servlet.xml當中的端口為80端口 maxThreads="150" minSpareThreads="25" maxSpareThreads="75" enableLookups="false" redirectPort="8443" acceptCount="100" connectionTimeout="20000" disableUploadTimeout="true" /> 好現(xiàn)在啟動tomcat訪問http://www.csdn.net/test/checkCookie.jsp 得到結果啦: this is www.csdn.net name=ABCDEF value=hbWhkUhCWfRTUyeAVO5k79qTBFZXtGV2qfwAROv%252fvVk9qF3rfkcZQbl1IdMsBxhy15Y%252fqwN2XiLyTy%252fDGuW4LY7zZll16huuVbnJ0CEun26I%252f2bynXLPd2Ymq%252bn2Mt11pSP5w3%252fZNXt9ZJEp79VMCw%253d%253d maxage=-1 domain=null secure=false#一般為默認值false不然為true就表示在像ssl驗證時才能訪問些cookie path=null 由于在本機不能創(chuàng)建cookie所以不能演示修改cookie值給大家看. 達內的論壇cookie 存放用戶名和密碼沒有經過加密的如 amembernamecookie javazhai#這里是我的用戶名 219.239.245.203/cgi-bin/ 1536 2476861952 29789838 3517958576 29783694 * apasswordcookies xxxxxx#這里是我的密碼 219.239.245.203/cgi-bin/ 1536 2476861952 29789838 3517958576 29783694 而csdn是經過加密方式存儲的如 ABCDEF hbWhkUhCWfRTUyeAVO5k79qTBFZXtGV2qfwAROv%252fvVk9qF3rfkcZQbl1IdMsBxhy15Y%252fqwN2XiLyTy%252fDGuW4LY7zZll16huuVbnJ0CEun26I%252f2bynXLPd2Ymq%252bn2Mt11pSP5w3%252fZNXt9ZJEp79VMCw%253d%253d 這一串肯定包含了用戶名和密碼的,它經過某種方式取得原始用戶名和密碼 |