ASP Cookies Lecture

xiaoxiao2021-03-06  128

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 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 (using the request command) from the visitor's computer ◆ Creating a cookie's basic syntax: 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. The Attribute parameter can be one of the following: 1Domain is specified, the cookie will be sent to the request of 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. ("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 following code, you can set the cookie's use of the use of "January 1, 2010": response.cookies ("cookiename"). EXPIRES = # January 01, the code after the 2010 #, will set the expiration of the cookie Time is "Cookie's Creation Time 365 days": Respires = Date 365, but it is best not to write response.cookies ("cookiename"). EXPIRES = DATE, this page is called The value will be empty. -------------------------------------------------- ------ Execute the following code will create a cookie, name = VisiTorname, value = ken: response.cookies ("visitorname") = "hen") = "visitorname" = "" " Create a cookie, name = visitor, value = value = value = list = value = value = reponse.cookies ("visitorname") = request.form ("username") You can extend the following code becomes a cookie subkey key value (cookiesubname) The generation of the cookie dictionary. The code is as follows: response.cookies ("visitorname") = "Ken" response.cookies ("trastname") = "baumbach" ------------- ------------------------------------------ ◆ Read the basic syntax of 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 Analysis: ◆ Num.asp (the first time the "First Access" is displayed for the first time by staying within one year, the first time to visit the "First Access" is displayed in the first few times. " Cookies ("Visitnum") if Num> "0" Thennum = NUM ​​ 1RESPONSE.WRITE "You are already in" & NUM & "times to access this site." Elseresponse.write "Welcome to this site." Num = 1END iFResponse.cookies ("VisitNum") = NumResponse.cookies ("VisitNum"). Expires = DATE 365%> ◆ ShowCookie.asp (from the cookies folder all the browser all cookie name, and related dictionary cookie Display (Blue Character Display)) <% for Each Cookie in Request.Cookiesif Request.Cookies (cookie) .Haskeys = false theresponse.write cookie & "=" Response.write ("") Elsefor Each Key In Request.cookies (cookie) response.write ("") response.write cookie & "" & "& recommended.cookies (cookie) (Cookie) (Cookie) (Cookie) (KEY) RESPONSE. Write ("") NEXTEND IFNEXT%> ◆ 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%>

bgcolor:
fgcolor:
password:
<% end if 'If the cookie already exists, and BGColor exists, jump to color.asp.

If BGCOLOR <> "" ThenResponse.Redirect "Color.asp" End IF%> ◆ Color.asp (Page Features Show User Hobbies Color) <% BGColor = Request.Cookies ("Check") ("BGCOLOR") fgcolor = Request.Cookies ("Check") PWD = Request.Cookies ("Check") ("PWD")%> <% response.write (" ")%> your password is: <% response.write ( "" & pwd & "")%> author: Bruce wolf

转载请注明原文地址:https://www.9cbs.com/read-126128.html

New Post(0)