I have wanted to write an article about cookies, especially the problem of the client side Script and Server's ASP through the cookie interaction may bother everyone. Actually
If you have a deep understanding of Cookie, especially if the concept of domain and path is clear, there will be no problem.
Another point I want to remind is that the results obtained by the request.servervariables (http_cookie) to get cookie and request.cookies may be different.
Everyone thinks why it will help the learning of ASP.
Request.cookies, particles:
<Table border = 2>
<Thead>
<th> cookie name </th>
<th> cookie value </th>
<th> Cookie Haskeys </th>
</Thead>
<%
Dim item
For Each item in request.cookies
%>
<Tr>
<Td> < % = item %> </td>
<Td> < % = request.cookies (item) %> </td>
<Td> < % = request.cookies (it). Haskeys %> </td>
</Tr>
<Tr>
<%
If request.cookies (it) .haskeys then
For Each Strsubkey in Request.cookies (item)
%>
<TD> & bnsp; </td>
<TD> & bnsp; </td>
<Td> < % = Request.cookies (Strkey) (Strsubkey) %> </td>
<%
Next
End if
Next
%>
</Table>
request.servervariables (http_cookie), particles:
<Table border = 2>
<Thead>
<th> cookie name </th>
<th> cookie value </th>
</Thead>
<%
Dim item, sp, i, d
sp = split (request.servervariables (http_cookie),;, -1,1)
LOOP Through the Cookie Collection Displaying Each Cookie We Find
For i = 0 to ubound (SP)
D = split (cstr (sp (i)), =, -1,1)
%>
<Tr>
<Td> < % = d (0) %> </td>
<TD> < % if ubound (d) = 1 then response.write (d (1)) else response.write & nbsp; %> </td>
</Tr>
<%
Next
%>
</Table>
Heavy particle@y2k0814
------------------------------------------------ -------
Below is the <SSP COOKIE Guide Guide> Translation of Gan Jiping>
In fact, in web development, Cookie is just a text file. When a user access the site, it is stored on the computer used by the user. Among them, it is stored
Some information, when users visit this site in the future, web can extract this information.
Although there is no excitement of Cookie now, you can actually use it to achieve many meaningful functions! For example: you can place it on the site
A question and answer form, ask the visitors' favorite colors and fonts, and then based on the web interface of these customized users. And, you can also save the visitor's login password, this
In the case, when the visitors visit this site again, they do not need to enter a password to log in.
Of course, cookies also have some shortcomings. First of all, because the function of cookies can be programmed to achieve some bad attempts, most browsers have a safe setting
It is fixed, where you can set whether to allow or accept cookies, so this cannot guarantee that cookies can be used at any time. Furthermore, visitors may deliberately or inadvertently delete
Cookie. When the visitor's machine encounters the blue screen crash, or after re -formatting the hard disk and installation system, the original cookie will be lost. The last point, yes
Some initial browsers do not support Cookie.
What can I do with cooklie?
There are two basic ways to use cookies:
1. Write cookie to the visitor's computer (using the Response command)
2. Cookie from the visitor's computer (using the Request command)
Cook the basic grammar of cookies
Response.cookies (cookiename) = Value
The following code will create a cookie in the visitor's computer, name = visitorname, value = Ken
Response.cookies (Visitorname) = Ken
The following code will create a cookie in the visitor's computer, name = visitorname, value = the value of username in the form in the form.
Response.cookies (visitorname) = request.form (username)
Read the basic grammar of cookie
Request.cookies (cookiename)
You can treat the request value as a variable. After executing the code below, the Cookie value of the name of KensCookie will be retrieved, and the variable Myvar is stored in the variable:
Myvar = Request.Cookies (KensCookie)
After executing the following code, the Cookie value of the judgment of the name of KensCookie is yes:
If Request.Cookies (KensCookie) = Yes then
Functional Cookie
You can expand the above code to become the key value of cookies. The code is as follows:
Response.cookies (visitorname) (firstName) = Ken
Response.cookies (Visitorname) (LastName) = Baumbach
Before explaining the example, discuss two concepts: command agreed and expired time.
Naming
Like the names of other variables, it is appropriate and uniquely named cookie, which is conducive to using it coherent in the program. You can use 1 or 2 below
Cookie attributes are named COOKIE variables:
Domain attributes: The domain attribute indicates which website is generated or read from which website. By default, the domain attribute of the cookies is set to
You can also change it as needed. The relevant code is as follows: response.cookies (cookiename) .domain = www.mydomain.com
Path attributes (PATH): The path attribute can achieve more security requirements. By setting the accurate path on the website, the scope of the cookies can be limited. For example:
Response.cookies (cookiename) .path =/maInDir/Subdir/Path
Used time
Usually, when the browser is closed, a cookie does not exist. But in many times, for example, the web site examples to be discussed below, we hope to change
Save cookie on the visitor's computer for a long time. Fortunately, there are such implementations. The following code can be set to set the use of cookies
January 1:
Response.cookies (cookiename) .expires =#January 01, 2010#
After executing the following code, the expiration time of the cookie is set to the creation time of cookies + 365 days:
Response.cookies (cookiename) = date+365
Use the actual example of cookies (very exciting)
Now start discussing actual examples. Assuming: You want to do a survey. Everyone needs to fill in the information when visiting for the first time, but when you visit again, you don’t need to be there anymore
What do you do. Using Cookie can solve this problem very successfully, but you don't have to use the database.
< %@ Language = VBScript %>
< %
Survey = Request.cookies (KensSurvey)
If survey = then
Response.cookies (key) = x
Response.cookies (key) .expires =#January 01, 2010#
Response.redirect Survey.asp
Else
rest of the page
End if
%>
Okay, let's discuss the code above from scratch.
First, the initial setting page, and read the cookie value of the name of KensSurvey:
< %@ Language = VBScript %>
> < %
Survey = Request.cookies (KensSurvey)
Then, judge whether there is a cookie value:
If survey = then
If it does not exist, create and set up cookie and transfer to the page Survey.asp. During the current visit, because there is a cookie value, it will not be transferred to
Survey.asp page.
Response.cookies (key) = x
Response.cookies (key) .expires =#January 01, 2010#
Response.redirect Survey.asp
If the cookie already exists, the visitor will execute the remaining code in the page:
rest of the page
End if
%>
Example 2
Here is another simple example: when the visitor browses a certain site for the first time, shows them welcome information. The code is as follows:
< %@ Language = VBScript %>
< %
Requestname = request.form (name)
RequestLaveMealone = Request.form (LeaveMealone)
If Requestname <> or RequestLavemealone <> THEN
Response.cookies (mysitevisitorName) = Requestname
Response.cookies (mysitevisitorName) .expires = #january 01, 2010 #
Response.cookies (mysiteleavemealone) = RequestLaveMealone
Response.cookies (mysiteleavemealone) .expires = #january 01, 2010 #
End if
Visitorname = request.cookies (mysitevisitorname)
Leavemealone = request.cookies (mysiteleavemealone)
If visitorname = and leavemealone = then
%>
<Html> <head> < /head>
<body bgcolor =#ccfffff text = black link = navy vlink = purple>
<Div align = center>
<FORM ACTION = Index.asp Method = Post>
<H2> Lets be friend < /h2>
Whats your name (Leave Blank and Hit the Submit Button if you Dont WANT US to Know)?
<input type = text name = name> <br> <br>
<input type = hidden name = leaveMealone value = x>
<input type = submit value = submit>
< /Form>
< /Div>
< /Body>
< %
End if
If visitorname <> then
Response.write hi, & visitorname &! I hope you are having a great day!
End if
rest of the page
%>
Okay, let's take a look at what the code above is implemented. First, set the page and check the form variable (in the same page). If the form variable is stored
In the creation of cookies and setting the expiration time.
< %@ Language = VBScript %>
< %
Requestname = request.form (name)
RequestLaveMealone = Request.form (LeaveMealone)
If Requestname <> or RequestLavemealone <> THEN
Response.cookies (mysitevisitorName) = Requestname
Response.cookies (mysitevisitorName) .expires = #january 01, 2010 #
Response.cookies (mysiteleavemealone) = RequestLaveMealone
Response.cookies (mysiteleavemealone) .expires = #january 01, 2010 #
End if
Then, read cookie:
Visitorname = request.cookies (mysitevisitorname)
Leavemealone = request.cookies (mysiteleavemealone)
If Cookie does not exist on the visitor's computer, create a form and ask the relevant information:
If visitorname = and leavemealone = then
%>
<Html>
<Head>
< /Head>
<body bgcolor =#ccfffff text = black link = navy vlink = purple>
<Div align = center>
<FORM ACTION = Index.asp Method = Post>
<H2> Lets be friend < /h2>
Whats your name (Leave Blank and Hit the Submit Button if you Dont WANT US to Know)?
<input type = text name = name> <br> <br>
<input type = hidden name = leaveMealone value = x>
<input type = submit value = submit>
< /Form>
< /Div>
< /Body>
< %
End if
If the cookie exists and the user name exists, it will be displayed to the visitor a welcome interface, and then executes the remaining code.
If visitorname <> then
Response.write hi, & visitorname &! I hope you are having a great day!
End if
rest of the page
%>
Although the above example is simple, many creative applications can be expanded from it. You can add many functions to the form for customized web sites.
You can also let visitors customize the color and fonts of the website, so that other web elements. If possible, you can ask the visitors’s birthday, when the visitors visit that day
At that time, you can show him a happy birthday message.
As you can see, the scalability of Cookie is endless, and this article is just a brick.