This article introduces the basic syntax and usage of Request. Cookies. The usage of Request. Cookies is analyzed through examples.
The Request. Cookies method can be used to retrieve the values in the Cookies set. The Cookies set allows users to retrieve the values of Cookies sent in HTTP requests.
Request. cookie Syntax: Request. Cookies (cookie) [(key) |. attribute]
Cookies are a tag that is embedded in the user's browser by the WEB server to identify users. The next time a browser requests a webpage, it will send Cookies received from the WEB server.
Cookies can be used to store user-related information. The Cookies set of Response and Request objects can be used to set or obtain the Cookies.
Use the Cookies set of the Response object to set the usage of Cookies:
Response. Cookies (CookieName) [(Key) |. Attribute] = value
Parameter description:
(1) CookieName: indicates the Cookie name.
(2) Key: the Key specified by Cookie. By specifying a keyword for a Cookie, the Cookie can have multiple values. This Cookie is called an indexed Cookie.
(3) Attribute: this parameter indicates the attributes of the Cookies set object. The most common Attribute is Expires (used to set the cookie expiration date), Domain (used to set the valid Domain name range of Cookies ).
For example, if you want to set a Cookie named VisitNum to store the number of times the user visits the website and initialize its value to 1, the implementation code is:
<% Response. Cookies ("VisitNum") = 1%>
The statement must be inBefore marking. If the specified cookie already exists, the cookie value is overwritten. If the cookie does not exist, the cookie is created.
Cookies configured using this method are valid only in the current session. If the cookie is valid after the user has terminated or restarted the browser, the browser must be forced to save the Cookie to the user's hard disk. Therefore, you can set the Expires attribute of the Cookie set object for a certain period of time.
For example, to set a cookie named VisitNum to expire in 5 days, set it:
<%
Response. Cookies ("VisitNum") = 1
Response. Cookies ("VisitNum"). Expires = DateAdd ("d", 5, Date)
%>
For example, you can use a Cookie to save the number of times a netizen accesses the website and the date and time of the last visit. The Cookie must expire seven days later.
There are two pieces of information to be saved, and these two pieces of information are related to each other. Therefore, they can be implemented using Cookies with indexes. In addition, two different cookies can be used to save the data separately.
To accumulate the number of accesses, you can use the Cookie set object of the Request object to obtain the Cookie value. After increasing, the Cookie is assigned back. The Request Method for Cookie operations is similar to Response, except that Request is used to obtain the Cookie value, while Response is used to set the Cookie value.
(1) The implementation of Cookies with indexes is as follows:
<%
If Request. Cookies ("UserVisit") ("num") = "" then' if the Cookie does not exist
Response. Cookies ("UserVisit") ("num") = 1' create the Cookie and assign the Initial Value
Else
Response. Cookies ("UserVisit") ("num") = Request. Cookies ("UserVisit") ("num") + 1
End if
Response. Cookies ("UserVisit") ("LastVisit") = now
Response. Cookies ("UserVisit"). Expires = DateAdd ("ww", 1, Date)
%>
To test whether the Cookie is set correctly, you can use the following code to output the corresponding Cookie value:
<% = "Number of visitors:" & Request. Cookies ("UserVisit") ("num ")&"
Last visit: "& Request. Cookies (" UserVisit ") (" LastVisit ") %>
(2) implementation with two different cookies (without indexes ):
<%
Response. Cookies ("VisitNum") = 1
Resposne. Cookies ("VisitNum"). Expires = DateAdd ("ww", 1, Date)
Response. Cookies ("LastVisit") = now
Response. Cookies ("LastVisit"). Expires = DateAdd ("ww", 1, Date)
%>
After reading this article, I believe that you have a preliminary understanding of Request. Cookies, and you will have a deep understanding of Request. Cookies by writing several examples on your own.