Request. Cookies and response. Cookies Analysis)

Source: Internet
Author: User
Tags send cookies

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, to set a cookie named visitnum to store the number of times the user visits the website and initialize the value to 1CodeIs:

<% Response. Cookies ("visitnum") = 1%>

The statement must be placed before the <HTML> mark of the webpage. 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") & "<br> 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)
%>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.