Detailed introduction to ASP Cookies and instance code

Source: Internet
Author: User

Cookie is a text string handle sent to the client's browser and saved on the client's hard disk. It can be used to persistently maintain data between sessions of a Web site. Both Request and Response objects have a set of cookies. The Request. cookie set is a series of cookies that are sent from the client and HTTP Request to the Web server. If you want to send the Cookie to the client, you can use Response. cookie
1. ExpiresAbsolute attributes
This attribute can be assigned a date. After this date Cookie is passed, it will no longer be used. You can delete a Cookie by assigning an expiration date to the Expires attribute. For example:
<% Response. cookies ("passtime"). expiresAbsolute = "1/1/99" %>
2. Domain attributes
This attribute defines the unique domain that the Cookie will send. For example, if a Cookie is sent to only Microsoft users, you can use the following code.
<% Response. Cookies ("domain"). Domain = "www.microsoft.com" %>
3. the syntax used by ASP to write a Cookie is as follows: 
Response. Cookie ("Cookie name"). [("key name"). Attribute] = content
If an ASP file creates a Cookie, the following code can be placed before the first <% Response. Cookies ("CookieName") = "NewCookie" %>
<Html>
......
</Html>
4. ASP uses the Cookie set of the Request object to read the Cookie, for example:
<% Response. write Request. Cookies ("CookieName") %>
The following uses a complete example to describe Cookie: Copy codeThe Code is as follows: <%
Dim Num
Num = Request. Cookies ("Visit_num ")
If Num> 0 then
Num = Num + 1
Response. write "you have accessed this site for" & Num. "
Else
Response. write "welcome to visit this site for the first time. "
Num = 1
End if
Response. Cookies ("Visit_num") = Num
%>

In this example, read the cookie variable Visit_num to check whether the user-side computer stores the cookie variable. If this variable exists, the user has accessed the page and entered the number of visits. If the user visits this page for the first time, there will be no cookie variable in the computer, the program will display the "welcome" text, and then save the cookie variable Visit_num to the user's computer, this allows the user to provide "number of visits" during the next visit to the page.
5. Cookie dictionary
Sometimes many COOKIE variables may need to be defined on a page. In order to better manage them, the one-person sub-key concept is often introduced in Cookies ". The syntax for referencing it is as follows:
Request. Cookies ("Change name") ("subkey name ")
The following Cookie creates a Dictionary named "Dictionary" and stores three key values:Copy codeThe Code is as follows: <%
Response. Cookie ("info") ("Myname") = "jeff"
Response. Cookie ("info") ("Gender") = "male"
Response. Cookie ("info") ("Myheight") = "172"
%>

In fact, the Cookie dictionary on the client exists as a string:
Info = Myname = jeff & Gender = male & Myheight = 172.
If you directly reference the cookie variable without specifying a subkey name, a string containing all the subkeys and values is returned. For example, the preceding example contains three "subkeys": "Myname", "Gender", and "Myheight". When a user does not specify a "subkey", the Request is directly sent. when Cookies ("info") are referenced, the following strings are obtained:
Info = Myname = jeff & Gender = male & Myheight = 172.
To read all the data in the Cookie, use the following code:Copy codeThe Code is as follows: <% For each cookie in Request. Cookies
If Not cookie. HasKeys then
Response. write cookie & "=" & Request. Cookies (Cookies)
Else
For each key in Request. Cookies (Cookies)
Response. write cookie & "(" & key & ")" & "=" & Request. cookie (key)
Next
End if
Next
%>

The following code records the queried records on the pageCopy codeThe Code is as follows: Sub SetCookie
Dim C_DomainList, C_ I
C_DomainList = Request. Cookies ("jb51") ("C_DomainList ")
If Domain <> "" and C_DomainList <> "" then
If not instr (C_DomainList, Domain & "|")> 0 then C_DomainList = Domain & "|" & C_DomainList
End if
If Domain <> "" and C_DomainList = "" then
C_DomainList = Domain & "|"
End if
If C_DomainList <> "" then
Response. write "<div id = C_domainlist> your site :"
C_arrDomain = split (C_DomainList, "| ")
C_DomainList = ""
NumDomain = ubound (C_arrDomain)-1
If numDomain> 4 then numDomain = 4
For C_ I = 0 to numDomain
Response. write "<a href =? Url = "& C_arrDomain (C_ I) &"> "& C_arrDomain (C_ I) &" </a> |"
C_DomainList = C_DomainList & C_arrDomain (C_ I) & "|"
Next
Response. Cookies ("jb51") ("C_DomainList") = C_DomainList
Response. Cookies ("jb51"). Expires = Date + 30
Response. write "<a href = # style =" "cursor: pointer" "onClick =" "clearCookie ('jb51'); alert ('cleared records! '); ""> Clear records </a> </div>"
End If
End Sub

Demo page: http://tools.jb51.net/whois.asp

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.