Cookie Gan Ganping Translation <asp cookies use guide >
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, which saves the
Some information, the Web can extract this information when the user accesses 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 of all, because of the ability to use cookies can be programmed to implement some bad intentions, so most browsers have security
Set, where cookies are allowed or accepted, so there is no guarantee that cookies can be used at any time. Furthermore, visitors may intentionally or unintentionally delete
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. Last point, there
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: Field 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 Properties (PATH): Path properties can achieve more security requirements by setting the exact path on the Web site to limit the use of cookies. 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, such as the Web site example that will be discussed below, we hope to be more
Save cookies for a long time on the visitor's computer. Luckily, there is a way to achieve this. The following code allows you to set the expiration time of a cookie to 2010 years
January 1:
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 (very exciting)
Now let's discuss the actual example. Suppose: You want to do a survey, everyone needs to fill out the first visit, but after the same day to visit, there is no need for that
Do. 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. On the next visit, because of the existence of a cookie value, it is not transferred to the
Survey.asp page.
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")