What is cookie? Cookie is actually a label, often might hear Chinese translation: small 舔. When you visit a web site that needs to uniquely identify your site, it will leave a tag on your hard drive. When you visit the same site, the site will look up this tag.
Each Web site has its own tag, and the content of the mark can be read at any time, but can only be done by the page of the site. Each site cookie has a different file in the same folder in the cookies of all other sites (you can find them in the cookie folder in Win98's Windows directory, while Win2K is in the Documents and Settings folder specific users Cookies folder).
A cookie is a unique identity of the customer's tag, and the cookie can contain information shared by all pages of a web site between a dialog or several dialogues, using cookies to exchange information between the pages. This feature is often used in an ASP program that requires authentication customer passwords and electronic bulletin boards, web chat rooms.
Although it sounds no exciting, it is actually used, you can actually use it. You can achieve many meaningful features! For example: You can place a survey questions on the site, ask the visitor's favorite color and font, and then based on these custom users' web interfaces. Also, you can save the visitor's login password so that when the visitor accesses this site again, no need to enter the password to log in.
Of course, cookies have some shortcomings. First, since it is possible to implement some bad attempts, most browsers have security settings, which can be set to allow or accept the cookie ("tool" in the IE browser - "Internet options .. "-" Security "-" Custom Level "-" Use "; Tools" in the Netscape Browser - "Cookie Manager" - "Manage Save Cookie", so this You cannot guarantee that you can use cookies at any time. Furthermore, the visitor may deliberately or unintentionally delete cookies. When the visitor's machine encounters the "blue screen" crash, or reformat the hard drive, after installing the system, the original saved cookie will be all lost. Last point, some of the initial browsers cannot support cookies.
◆ How to use the cooklie? There are two basic ways that use cookies: 1. Write cookies to the visitor's computer (using the response command) 2, retrieve the cookie from the visitor's computer (using the request command)
◆ Creating the basic syntax of cookie:
Response.cookies (cookie) [(key) | .attribute] = VALUE
The cookie here is the name of the specified cookie. And if Key is specified, the cookie is a dictionary. (Test if a cookie is available for a dictionary to display the Boolean value: <% = Request.cookies ("cookiename"). Haskeys%>. Is True is a dictionary, and false is not.) Attribute specifies the information about cookie itself. Attribute parameters can be one of the following:
1Domain is specified, the cookie will be sent to the request to the domain. Domain properties indicate which site is generated or read by cookies. By default, cookie's domain property is set to generate its website, but you can also change it as needed.
("Cookiename"). Domain = "www.mydomain.com") 2Path is a path attribute, which can realize more security requirements, can limit the use of cookies by setting an accurate path on the website. range. If this property is not set, the path to the application is used.
(Response.Cookies ("cookiename"). Path = "/ maindir / subdir / path")
3EXPIRES Specifies the expiration date of the cookie. In order to store the cookie on the client disk after the session, or in many cases, we hope to save cookies over the visitor's computer. This date must be set. If the settings of this attribute do not exceed the current date, the cookie will expire after the task is completed.
After the code, you can set the use of cookies to use "January 1, 2010":
Response.cookies ("cookiename"). EXPIRES = # january 01, 2010 #
The latter code will set the cookie's expiration time for "Cookie's creation time 365 days":
Response.cookies ("cookiename"). EXPIRES = DATE 365
But it is best not to write response.cookies ("cookiename"). EXPIRES = DATE, the call between the page is empty.
Execute the following code will create a cookie in the visitor's computer, name = visitor, value = ken:
Response.cookies ("VisitorName") = "ken"
Performing the following code will create a cookie, name = visitorname, value = value in the form in the visitor's computer
Response.cookies ("visignorname") = Request.form ("UserName")
You can expand the following code to become a cookie sub-key value (cookie dictionary). code show as below:
Response.cookies ("firstname") = "ken"
Response.cookies ("LastName") = "baumbach"
◆ Read the basic syntax of the cookie: Request.Cookies (cookie) [(key) | .attribute] cookie Specifies the cookie to retrieve its value. Key Optional parameters for retrieving the value of the sub-key from the Cookie dictionary. AttribE specifies information about cookie itself. Such as: Haskeys read-only, specify whether cookies contains keywords. If the client browser sends two favorite cookies, then Request.cookie will return one of the deeper path structure. For example, if there are two favorite cookies, but one of the path properties is / WWW / and the other is / www / home /, the client browser simultaneously sends two cookies to / www / home / directory Then, Request.cookie will only return the second cookie. case study:
◆ Num.asp (the first time to visit "First Access" in the first time by staying in the cookie recorded on the local disk, the first time to display "First Access")
<%
DIM NUM
Num = Request.cookies ("Visitnum")
IF Num> "0" THEN
Num = NUM 1
Response.write "You are already" & Num & "times to visit this site."
Else
"Welcome to this site for the first time."
Num = 1
END IF
Response.cookies ("Visitnum") = NUM
Response.cookies ("VisitNum"). Expires = DATE 365
%>
◆ ShowCookie.asp (All cookie names from the cookies folder ", all the browser, and related dictionary cookie display (blue word display))
<%
For Each Cookie in Request.cookies
If Request.Cookies (cookie) .haskeys = false kil
Response.write cookie & "=" & request.cookies (cookie)
Response.write (""
")
Else
For Each Key In Request.cookies (cookie)
Response.write ("")
Response.write cookie & "(" & key & ")" & "=" & recommended.cookies (cookie) (KEY)
Response.write (" font>
")
NEXT
END IF
NEXT
%>
◆ Check.asp First, set the page. Then, check the form variable (in the same page). If you exist, create a cookie, and set the expiration time.
<% @Language = "VBScript"%> <% bgcolor = request.form ("bgcolor") fgcolor = request.form ("fgcolor") PWD = Request.form ("PWD") if bgcolor <> "" or fgcolor <> "" "" "" "") ("check") ("check") ("check") = fgcolorresponse.cookies ("check") ("pwd") = PwdResponse.cookies "Check"). EXPIRES = # May 01, 2004 # end if 'Next, read cookiebgolor = request.cookies ("check") ("check") ("check") ("fgcolor") PWD = Request.Cookies ("Check") 'If the cookie does not exist on the visitor's computer, create a form, ask related information if bgcolor = "" and fgcolor = "" and pwd = "" THEN%>
head>