Both 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 with the HTTP Request. Conversely, if you want to send a cookie to the client, you can use Response.cookie. A cookie is a text string handle that is sent to the client's browser and stored on the client's hard drive. It can be used to persist data between sessions of a Web site. Both 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 with the HTTP Request. Conversely, if you want to send a cookie to the client, you can use Response.cookie
1. ExpiresAbsolute attribute
This attribute can be assigned a date, after which the cookie can no longer be used. Cookies can be deleted by assigning an expiration date to the Expires attribute. like:
<%Response.cookies(passtime).expiresAbsolute=1/1/99%>
2. Domain attribute
This attribute defines the unique domain to which the cookie is sent. For example: Cookies are only sent to Microsoft people, you can use the following code.
<%Response.Cookies(domain).Domain=www.microsoft.com%>
3. The syntax used by ASP to write Cookie and send Cookie to the client is as follows:
Response.Cookie(Cookie name).[(key name).Attribute]=content
If an ASP file wants to create a cookie, the following code can be placed before the first <html> of the ASP file to avoid errors.
<%Response.Cookies(CookieName)=NewCookie %>
<html>
...
</html>
4. Similarly, ASP uses the Cookies collection of the Request object to read Cookies, such as:
<%Response.write Request.Cookies(CookieName)%>
Below is a complete example to illustrate Cookie:
Copy the code code as follows:
<%
dim Num
Num=Request.Cookies(Visit_num)
if Num>0 then
Num=Num+1
Response.write You have visited this site for the &Num&th time.
else
Response.write Welcome to your first visit to this site.
Num=1
end if
Response.Cookies(Visit_num)=Num
%>
In this example, the Cookies variable Visit_num is first read to see whether the Cookies variable is saved on the client computer. If this variable exists, it means that the user has visited the page and enter the number of visits. If the user visits this page for the first time, there will be no Cookies variable in his or her computer. The program will display the welcome word, and then save the Cookies variable Visit_num to the user's computer so that the number of visits will be given the next time the user visits the page. .
5. Cookie Dictionary
Sometimes it may be necessary to define many Cookies variables in a page. In order to better manage them, the concept of a person's subkey is often introduced in the Cookies component. The syntax for referencing it is as follows:
Request.Cookies(change name)(subkey name)
For example, the following Cookie creates a dictionary named Dictionary, which stores three key values:
Copy the code code 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 computer exists in the form of a string:
info=Myname=jeff&Gender=male&Myheight=172
If the user does not specify a subkey name and directly references the Cookies variable, a string containing all subkey names and values will be returned. For example, the above example contains three subkeys: Myname, Gender and Myheight. When the user does not specify the subkeys and refers directly through Request.Cookies(info), the following string will be obtained:
info=Myname=jeff&Gender=male&Myheight=172
If you want to read all the data in the cookie, you can use the following code to get it:
Copy the code code as follows:
<%For each cookie in Request.Cookies
if Not cookie.HasKeys then
Response.write cookie & = & Request.Cookies(cookie)
Else
for each key in Request.Cookies(cookie)
Response.write cookie&(&key&)&=& Request.Cookies(cookie)(key)
next
end if
next
%>
The following is the specific code for recording the query records on the page.
Copy the code code 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>Site you are interested in:
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('Record cleared!');>Clear record</a></div>
End If
End Sub