In fact, in web development, a cookie is just a text file, and when a user accesses a site, it is stored on the computer that the user is using, where the information is saved and the Web can extract the information when the user visits the site again later.
Although it sounds a little exciting now, you can actually use it to achieve a lot of meaningful functionality! For example: You can place on the site
A questionnaire that asks visitors what colors and fonts they like best, and then according to the Web interface of these custom users. And, you can also save the visitor's login password, which
, when visitors visit this site again, do not need to enter a password to log in.
Of course, cookies also have some deficiencies. First, because cookies can be programmed to implement some bad intentions, most browsers have a security setting in which you can set whether cookies are allowed or accepted, so there is no guarantee that cookies can be used at any time. Furthermore, a visitor may intentionally or unintentionally delete a cookie. The original saved cookie will be lost when the visitor's machine encounters a "blue screen" panic, or reformatting the hard drive and installing the system. Finally, some of the most original browsers do not support cookies.
what can you do with Cooklie?
There are 2 basic ways of using cookies:
1. Write cookies to the visitor's computer (using the RESPONSE command)
2. Retrieve cookies from the visitor's computer (using the REQUEST command)
basic syntax for creating cookies
Response.Cookies ("CookieName") =value
Executing the following code will create a cookie, name =visitorname, and value =ken on the visitor's computer
Response.Cookies ("visitorname") = "Ken"
Executing the following code will create a cookie on the visitor's computer, the name =visitorname, the value = the value of the username in the form
Response.Cookies ("Visitorname") =request.form ("UserName")
basic syntax for reading cookies
Request.Cookies ("CookieName")
You can treat the request value as a variable, execute the following code, retrieve the cookie value named Kenscookie and deposit it in the variable myvar:
Myvar=request.cookies ("Kenscookie")
Execute the following code to determine whether the cookie value named Kenscookie is "Yes":
If request.cookies ("kenscookie") = "Yes" then ...
feature-rich cookies
You can extend the above code to become a cookie subkey key value (Cookiesubname), the code is as follows:
Response.Cookies ("Visitorname") ("FirstName") = "Ken"
Response.Cookies ("Visitorname") ("LastName") = "Baumbach"
Before explaining the example, the last 2 concepts are discussed: Command conventions and usage expiration times.
Naming Conventions
As with other variables, naming cookies appropriately and uniquely helps to use it consistently in your program. You can use the following 1 or 2
Cookie property to name the cookie variable:
Domain Properties (domains):The domain properties indicate which Web site The cookie is generated from or read, by default, the domain property of the cookie is set to produce its web site, but you
You can also change it as needed. The relevant code is as follows: Response.Cookies ("CookieName"). Domain = "Www.mydomain.com"
path Attribute (path):The path attribute enables more security requirements and limits the use of cookies by setting the exact path on the Web site. For example:
Response.Cookies ("CookieName"). Path = "/maindir/subdir/path"
Use expiration Time
Typically, a cookie does not exist when the browser is closed. But in many cases, like the Web site example that will be discussed below, we want to save cookies for a longer time on the visitor's computer. Luckily, there is a way to achieve this. The following code allows you to set the use expiration date for a cookie to January 1, 2010:
Response.Cookies ("CookieName"). expires= #January 01, 2010#
Execute the following code to set the expiration time of the cookie to "+365 days of the cookie creation time":
Response.Cookies ("CookieName") =date+365
practical examples of using cookies
Now let's discuss the actual example. Suppose you want to do a survey, each person needs to fill out the information when they first visit, but you don't need to do that again when you visit later in the day. With cookies, you can solve this problem very satisfactorily, and you don't need to use a database.
<%@ language= "VBSCRIPT"% >
<%
Survey=request.cookies ("Kenssurvey")
If Survey = "" Then
Response.Cookies ("Kenssurvey") = "X"
Response.Cookies ("Kenssurvey"). expires= #January 01, 2010#
Response.Redirect "Survey.asp"
Else
' Rest of the ' page
End If
% >
OK, let's start with the above code from the beginning.
First, the page is initially set and the cookie value named Kenssurvey is read:
<%@ language= "VBSCRIPT"% >
<%
Survey=request.cookies ("Kenssurvey")
Then, determine if the cookie value already exists:
If Survey = "" Then
If it does not exist, create and set a cookie and go to the page survey.asp. The next time you visit, you will not go to the survey.asp page because the cookie value exists.
Response.Cookies ("Kenssurvey") = "X"
Response.Cookies ("Kenssurvey"). expires= #January 01, 2010#
Response.Redirect "Survey.asp"
If the cookie already exists, the visitor executes the remaining code in the page:
' Rest of the ' page
End If
%>
Example 2
Here's another simple example: When a visitor browses a site for the 1th time, show them the welcome message. The code is as follows:
<%@ language= "VBSCRIPT"% >
<%
Requestname = Request.Form ("Name")
Requestleavemealone = Request.Form ("Leavemealone")
If Requestname < > "" or Requestleavemealone < > "" Then
Response.Cookies ("mysitevisitorname") = Requestname
Response.Cookies ("Mysitevisitorname"). Expires = #January 01, 2010#
Response.Cookies ("mysiteleavemealone") = Requestleavemealone
Response.Cookies ("Mysiteleavemealone"). Expires = #January 01, 2010#
End If
Visitorname = Request.Cookies ("Mysitevisitorname")
Leavemealone = Request.Cookies ("Mysiteleavemealone")
If visitorname = "" and Leavemealone = "" Then
% >
< HTML > < head >
< body bgcolor= "#ccffff" text= "Black" link= "Navy" vlink= "Purple" >
< DIV align= "CENTER" >
< form action= "index.asp" method= "POST" >
< H2 >let ' s be friends
What ' s Your name (leave blank and hit the Submit button if you don ' t 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 have a great day! "
End If
' Rest of the ' page
% >
OK, now let's see what the code implementation is doing. First, set the page. Then, check the form variables (on the same page). If the form variable exists, the cookie is created and the expiration time is set.
%@ language= "VBSCRIPT"% >
<%
Requestname = Request.Form ("Name")
Requestleavemealone = Request.Form ("Leavemealone")
If Requestname < > "" or Requestleavemealone < > "" Then
Response.Cookies ("mysitevisitorname") = Requestname
Response.Cookies ("Mysitevisitorname"). Expires = #January 01, 2010#
Response.Cookies ("mysiteleavemealone") = Requestleavemealone
Response.Cookies ("Mysiteleavemealone"). Expires = #January 01, 2010#
End If
Next, read the cookie:
Visitorname = Request.Cookies ("Mysitevisitorname")
Leavemealone = Request.Cookies ("Mysiteleavemealone")
If the cookie does not exist on the visitor's computer, create a form that asks for relevant information:
If visitorname = "" and Leavemealone = "" Then
% >
< HTML >
< head >
< body bgcolor= "#ccffff" text= "Black" link= "Navy" vlink= "Purple" >
< DIV align= "CENTER" >
< form action= "index.asp" method= "POST" >
< H2 >let ' s be friends
What ' s Your name (leave blank and hit the Submit button if you don ' t 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 already exists and the user name exists, it is displayed to the visitor with a welcome interface and then executes the rest of the 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, there are many creative applications that can be extended from it. You can add a lot of functionality to your form to customize your Web site.
You can also allow visitors to customize the colors, fonts, and other web elements of the site. If possible, you can ask the visitor's birthday when the visitor visits on that day
, you can display a "Happy Birthday" message to him.
As you can see, the extension of the cookie is infinite, this article is just a tip.