///
我们学习了可以用jsp Cookie类来创建cookie,当然其它语言也可以创建包括客户端脚本语言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 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 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来检查用户浏览器是否支持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来检查用户浏览器是否支持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 <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文件里面的内容的含义!!!!!!!
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.
好现在还看看其它方面的,有趣的一面!
在window当中cookie一般存放在C:\Documents and Settings\wwwfox\Cookies
wwwfox为我登录xp的账号,当然由于ie版本不同存放的位置也有所不同,你可以到微软官方网站上去查看,当然最好的方法就是搜一下Cookie文件就可以知道具体在存放在那里了,
上一页 [1] [2] [3] 下一页