ASP's detailed explanation of cookie operation

Source: Internet
Author: User
Tags date end http request key send cookies variable domain client
A cookie cookie is a text string handle sent to a client browser and saved on the client's hard disk to persist data between sessions of a Web site. Both the request and response objects have a set of cookies. The Request.cookie collection is a series of cookies sent from the client to the Web server, along with the HTTP request. Conversely, if you want to send cookies to the client, you can use the Response.cookie
1, ExpiresAbsolute Property
This property can be assigned a date, and after that date the cookie cannot be used again. You can delete a cookie by assigning an expiration date to the Expires property. Such as:
<%response.cookies ("Passtime"). Expiresabsolute= "1/1/99"%>
2, Domain properties
This property defines the unique domain that the cookie is to be routed. For example, the following code can be used if the cookie is sent only to people in Microsoft.
<%response.cookies ("Domain"). domain= "www.microsoft.com"%>
3, the ASP is used to write cookies to the client to send cookies The syntax is as follows:
Response.cookie ("Cookie name"). [(Key name]. property]= Content
If an ASP file is to create a cookie, the following code can be placed before the first <%response.cookies ("cookiename") = "Newcookie"%>
......
4, the same ASP use the Request object's Cookies collection to read cookies, such as:
<%response.write request.cookies ("CookieName")%>
The following is a complete example to illustrate cookies:
<%
Dim Num
Num=request.cookies ("Visit_num")
If Num>0 Then
Num=num+1
Response.Write "You have been to the first" & Num & "Time to visit this site. "
Else
Response.Write "You are welcome to visit this site for the first time. "
Num=1
End If
Response.Cookies ("Visit_num") =num
%>
In this example, the cookie variable visit_num is first read to see if the client computer holds the cookie variable. If this variable is available, the user has already visited the page and entered the number of visits. If the user is accessing the page for the first time, the program will display the word "welcome" and then save the cookie variable visit_num to the user's computer so that the user can give the "number of visits" information the next time the page is accessed.
5. Cookie Dictionary
Sometimes in a page may need to define a lot of cookies variable, in order to better manage it, in the cookie component often introduce a person's concept "subkey." The syntax for referencing it is as follows:
Request.Cookies ("Change Name") ("sub-key Name")
As the following cookie creates a dictionary named "Dictionary", which holds three key values:
<%
Response.cookie ("info") ("myname") = "Jeff"
Response.cookie ("info") ("Gender") = "male"
Response.cookie ("info") ("myheight") = "172"
%>
In fact, the cookie dictionary on the client is in the form of a string:
info=myname=jeff&gender=male&myheight=172
If the user does not specify a "subkey" name and refers directly to the cookie variable, a string containing all the "subkey" names and values will be returned. For example, the above example contains three "subkeys": "MyName", "Gender", and "myheight" that are referenced directly by request.cookies ("info") when the user does not specify their "subkeys":
info=myname=jeff&gender=male&myheight=172
If you want to read all the data in a cookie, you can use the following code:
<%for each cookie in Request.Cookies
If not cookie. HasKeys Then
Response.Write Cookies & "=" & Request.Cookies (Cookies)
Else
For each key in Request.Cookies (cookie)
Response.Write cookie& "(" &key& ")" & "=" & Request.Cookies (Cookie) (key)
Next
End If
Next
%>

Related Article

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.