]]>cookie技术与ƺ骗http://www.aygfsteel.com/zhaijianhui/archive/2006/05/19/46953.html蜘蛛蜘蛛Fri, 19 May 2006 00:42:00 GMThttp://www.aygfsteel.com/zhaijianhui/archive/2006/05/19/46953.htmlhttp://www.aygfsteel.com/zhaijianhui/comments/46953.htmlhttp://www.aygfsteel.com/zhaijianhui/archive/2006/05/19/46953.html#Feedback5http://www.aygfsteel.com/zhaijianhui/comments/commentRss/46953.htmlhttp://www.aygfsteel.com/zhaijianhui/services/trackbacks/46953.html什么是Cookie Cookie是一U在客户端保持HTTP状态信息的技术,它好比商场发攄优惠卡。顾客在一个商物结账离开Ӟ商场可以军_是否赠送给֮一张优惠卡Q不同顾客的优惠卡上记蝲的信息可以不同,例如Q记载该֮累计购物的金额和有效期限。顾客可以决定是否接受这张优惠卡Q一旦顾客接受了q张优惠卡,那么他在以后每次光顾该商场时Q都携带这张优惠卡Q商Z根据这张优惠卡上记载的信息q行一些特D的事务处理Q例如,计算折扣率和累加本次购物金额。Cookie是在览器访问WEB服务器的某个资源Ӟ由WEB服务器在HTTP响应消息头中附带传送给览器的一片数据,WEB服务器传送给各个客户端浏览器的数据是可以各不相同的。浏览器可以军_是否保存q片数据Q一旦WEB览器保存了q片数据Q那么它在以后每ơ访问该WEB服务器时Q都应在HTTPh头中这片数据回传给WEB服务器。显ӞCookie最先是由WEB服务器发出的Q是否发送Cookie和发送的Cookie的具体内容,完全是由WEB服务器决定的? WEB服务器通过在HTTP响应消息中增加Set-Cookie响应头字D将Cookie信息发送给览器,览器则通过在HTTPh消息中增加Cookieh头字D将Cookie回传lWEB服务器。一个Cookie只能标识一U信息,它至含有一个标识该信息的名UͼNAMEQ和讄|valueQ。一个WEB站点可以l一个WEB览器发送多个CookieQ这P在WEB览器和WEB服务器之间就可以使用多个Cookie来传递多U信息,例如Q用一个Cookie来标识访问者的姓名Q用另外一个Cookie来标识该用户d站点的次数。一个Cookie除了有名U和讄值外Q它q可以有一些其他的附加属性,例如QCookie的有效时间。如果设|了Cookie的有效时_接受它的览器进E将该Cookie保存在计机盘中,只有该Cookie出有效旉后才被删除,q样的Cookie被同一台计机上启动的多个览器进E共享。正如一个顾客可以有多家商场提供的优惠卡一P一个WEB览器也可以存储多个WEB站点提供的Cookie。ؓ了防止Cookie塞满客户机的盘Q浏览器一般只允许存放300个CookieQ每个站Ҏ多存?0个CookieQ每个Cookie的大限制ؓ4KB。如果没有设|Cookie的有效时_接受它的览器进E只该Cookie保存在自q内存I间中,在该览器进E关闭时Q它里面保存的所有Cookie也将随之消失? Cookie实现了一U在览器和服务器之间生有状态会话的方式Q它可以把一个浏览器讉K的同一个服务器上的所有程序诏qv来,在这些程序之间传递数据。例如,当用户用浏览器讉K某个|站的登录程序进行登录后Q无个浏览器再访问该|站的哪个程序,其他E序都能知道讉K者的w䆾信息Q这是在WEB站点中非常普遍的一个应用。这U应用通常是采用Cookie技术来实现的,当WEB服务器程序验证登录请求中的用户名和密码后Q生一个标识该用户w䆾的标识号Q然后在响应消息中将该标识号以Cookie的Ş式传递给览器,览器在以后每次讉K该WEB服务器时Q都自动在请求消息头中将标识号又以Cookie的Ş式返回给WEB服务器,凭借浏览器q回的标识号QWEB服务器的其他E序p分L出当前请求是由哪个用户发出的。但是,有一点要注意Q不保存在硬盘中的Cookie信息是否可以被同一台计机上启动的多个览器进E共享,不同的浏览器有不同的处理方式。对于IE览器来_保存在其中一个浏览器q程的内存空间中的Cookie是不能被其他览器进E共享的Q这׃出现同一台计机上的每个览器进E都会与服务器Ş成各自独立的会话Q而对于Mozilla Firefox览器来_所有的q程和标{N都共享cookie信息。另外,在IE览器中按Ctrl-N键(或者单几Z文件”;;“新建”;;“窗口”菜单)打开的窗口或者是用javascript的window.open语句打开的窗口,都会׃n原窗口的Cookie信息Q因为它们属于同一个浏览器q程内部的多个窗?张孝老师的<深入体验JavaWeb开发内q?gt;手稿很期待这本书的面? /// 我们学习了可以用jsp CookiecL创徏cookieQ当然其它语a也可以创建包括客L脚本语言javascript,vbscript同在我们主要谈谈用javascript,和html来创建cookie 1 Creating a Cookie that Is Valid Until a Certain Date <HTML> <HEAD> <TITLE>Creating a cookie that is valid until a certain date</TITLE> <META HTTP-EQUIV="Set-Cookie" CONTENT="userId=678;expires=Wednesday, 26-Dec-01 16:00:00 GMT; path=/"> </HEAD> <BODY> Unless you set your browser to not accept cookies, a cookie called userId with a value of 678 has been created for you. </BODY> </HTML> ///// 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 <HTML> <HEAD> <TITLE>Creating a cookie with document.cookiess</TITLE> <SCRIPT LANGUAGE="javascript"> document.cookiess="Quantity=7"; </SCRIPT> </HEAD> <BODY> This page creates a cookie on the client side. Make sure that your browser is set to accept cookies. </BODY> </HTML> 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 <SCRIPT LANGUAGE="javascript"> function setCookie(name, value, expires, path, domain, secure) { document.cookiess = name + "=" + escape(value) + ((expires) ? "; expires=" + expires.toGMTString() : "") + ((path) ? "; path=" + path : "") + ((domain) ? "; domain=" + domain : "") + ((secure) ? "; secure" : ""); } </SCRIPT> Listing 25.7 An Example that Creates a Cookie with an Expiration Date <HTML> <HEAD> <TITLE>Using the setCookie function</TITLE> <SCRIPT LANGUAGE="javascript"> function setCookie(name, value, expires, path, domain, secure) { document.cookiess = name + "=" + escape(value) + ((expires) ? "; expires=" + expires.toGMTString() : "") + ((path) ? "; path=" + path : "") + ((domain) ? "; domain=" + domain : "") + ((secure) ? "; secure" : ""); }
function fixDate(date) { var base = new Date(0); var skew = base.getTime(); if (skew > 0) date.setTime(date.getTime() - skew); }
var expiryDate = new Date(); fixDate(expiryDate); expiryDate.setTime(expiryDate.getTime() + 365 * 24 * 60 * 60 * 1000); setCookie("authorizationLevel", 2, expiryDate);
</SCRIPT> </HEAD> <BODY> A cookie which is valid for a year has been created for this page. </BODY> </HTML> Listing 25.9 Writing and Reading Cookies <HTML> <HEAD> <TITLE>Writing and Reading Cookies</TITLE> <SCRIPT LANGUAGE="javascript">
function getCookie(name) { var cName = name + "="; var dc = document.cookiess; if (dc.length>0) { begin = dc.indexOf(cName); if (begin != -1) { begin += cName.length; end = dc.indexOf(";", begin); if (end == -1) end = dc.length; return unescape(dc.substring(begin,end)); } } return null; }
</SCRIPT> </HEAD> <BODY> Type in your user id, and then click the Create Cookie button. A cookie will be created for you. <BR> <FORM> User ID: <INPUT TYPE=TEXT NAME=UserID> <BR> <INPUT TYPE=BUTTON value="Create Cookie" onClick='setCookie("UserID", document.FORMs[0].UserID.value)'> <BR> Click the Read Cookie button to display the cookie. <INPUT TYPE=BUTTON value="Read Cookie" onClick='alert(getCookie("UserID"))'> </FORM> </BODY> </HTML> Listing 25.10 Deleting a Cookie <SCRIPT LANGUAGE="javascript"> function deleteCookie (name, path, domain) { if (getCookie(name)) { document.cookiess = name + "=" + ((path==null) ? "" : "; path=" + path) + ((domain==null) ? "" : "; domain=" + domain) + "; expires=Thu, 01-Jan-70 00:00:01 GMT"; } }
function getCookie(name) { var cName = name + "="; var dc = document.cookiess; if (dc.length>0) { begin = dc.indexOf(cName); if (begin != -1) { begin += cName.length; end = dc.indexOf(";", begin); if (end == -1) end = dc.length; return unescape(dc.substring(begin,end)); } } return null; } </SCRIPT> Checking If the Browser Can Accept Cookies Using javascript 用javascript来检查用h览器是否支持cookie技? Listing 25.11 Checking If the Browser Can Accept Cookies Using javascript <HTML> <HEAD> <SCRIPT LANGUAGE="javascript"> document.cookiess="test=OK";
function getCookie(name) { var cName = name + "="; var dc = document.cookiess; if (dc.length>0) { begin = dc.indexOf(cName); if (begin != -1) { begin += cName.length; end = dc.indexOf(";", begin); if (end == -1) end = dc.length; return unescape(dc.substring(begin,end)); } } return null; }
if (getCookie('test')==null) alert("Please change your browser to accept cookies."); else alert("Browser accepts cookies");
</SCRIPT> </HEAD>
<BODY> The page content </BODY> </HTML> Checking If the Browser Accepts Cookies Without javascript 不用javascript来检查用h览器是否支持cookie技术!Q! 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 <META> 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 <HTML> <HEAD> <META HTTP-EQUIV="Set-Cookie" CONTENT="test=ok;"> <META HTTP-EQUIV="Refresh" CONTENT="0;URL=checkCookie.jsp"> </HEAD> </HTML> 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文g里面的内容的含义Q!Q!Q!Q? 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. 好现在还看看其它斚w的,有趣的一面! 在window当中cookie一般存攑֜C:\Documents and Settings\wwwfox\Cookies wwwfox为我dxp的̎?当然׃ie版本不同存放的位|也有所不同,你可以到微Y官方|站上去查看,当然最好的Ҏ是搜一下Cookie文g可以知道具体在存放在那里了, 下面是Cookies下面的一个Cookie文gwwwfox@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失? 后面是创徏日期,失效日期 创徏旉,失效旉 现在教你怎么用jsp~程查看上网时网站给你创建的cookie文gQ? 问题一.cookiess只能由它创徏的网站访问,那么我们怎么能够在自q电脑是运行自q服务器查看出来呢Q如果能成那是cookieƺ骗。R入别人网站的Ҏ? 好通过CZ来演C:, wwwfox@csdn[2].txt当然它的命名? 你的用户电脑帐号@产生的COOKIE的网|件所在的WEB目录[COOKIE改变的次数].txt wwwfox电脑帐号在访问csdn|站时是不会发送到csdn|站上去的,也就是只能在我的电脑wwwfox帐号上用这个cookie其hQ帐P是不能用的。? 在C:\Documents and Settings\wwwfox\Cookies下我有一个登录csdn|站时由它创建的cookie wwwfox@csdn[2].txt的内ҎQ? 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/<br>"); out.println("<br>"); for(int i=0;i<cookie.length;i++) { if(cookie
.getName().equals("ABCDEF")) { String name=cookie
.getName(); String value=cookie
.getvalue(); int maxage=cookie
.getMaxAge(); out.println("name="+cookie
.getName()+"<br>"); out.println("value="+cookie
.getvalue()+"<br>"); out.println("maxage="+cookie
.getMaxAge()+"<br>"); //out.println("setMaxAge?0"); //cookie
.setMaxAge(30); //out.println("之后为maxage="+cookie
.getMaxAge()+"<br>"); out.println("domain="+cookie
.getDomain()+"<br>"); out.println("secure="+cookie
.getSecure()+"<br>"); out.println("path="+cookie
.getPath()+"<br>"); } } %> 好现在访问一? http://localhost:8080/test/checkCookie.jsp是不是显C: this is
www.csdn.net 而没有取到|因ؓ只能在它的domain下能讉K到这个cookie 好现在我们来 把C:\WINDOWS\SYSTEM32\DRIVERS\etc下面的hosts.sam打开修改其中? 127.0.0.1 hostname ?27.0.0.1
www.csdn.net 在这里你q可以再d多行和上面相访的键值对Q这个比外网的dsn优先U高所以它先把www.csdn.net解释成本地的127.0.0.1所以你可以由q个讉K了,q也可以解释Z么我们可以在ie敲http://localhsot的原因,q里localhost你可随便Ҏ你喜Ƣ的名称. 保存 修改conf/servlet.xml当中的端口ؓ80端口 <Connector port="80" maxHttpHeaderSize="8192" maxThreads="150" minSpareThreads="25" maxSpareThreads="75" enableLookups="false" redirectPort="8443" acceptCount="100" connectionTimeout="20000" disableUploadTimeout="true" /> 好现在启动tomcat讉Khttp://www.csdn.net/test/checkCookie.jsp 得到l果啦: this is
www.csdn.net