A cookie is a text string handle sent to a client's browser and saved on the client's hard drive. It can be used to persist data between a certain Web site session. 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 together with HTTPRequest. In turn, if you want to send cookies to the client, you can use Response.cookie
1. ExpiresAbsolute property
This property can be assigned a date, and after this date, the cookie can no longer be used. Cookies can be deleted by assigning an expiration date to the Expires property. like:
<%Response.cookies("passtime").expiresAbsolute="1/1/99"%>
2. Domain attributes
This attribute defines the unique domain to which the cookie is to be delivered. 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 of ASP using to write cookies that is to send cookies 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 set of Request objects to read cookies, such as:
<%Response.writeRequest.Cookies("CookieName")%>
Here is a complete example to illustrate cookies:
<%
dimNum
Num=Request.Cookies("Visit_num")
ifNum>0then
Num=Num+1
Response.write "You have visited this site for the first time"&Num&"."
else
Response.write "Welcome to visit this site for the first time."
Num=1
endif
Response.Cookies("Visit_num")=Num
%>
In this example, first read the cookies variable Visit_num to see if the user computer saves the cookies variable. If this variable is present, it means that the user has visited the page and entered the number of visits at the same time. If the user visits the page for the first time, there will be no cookies variables in his computer. The program will display the word "Welcome" and then save the cookies variable Visit_num to the user's computer so that the user will give the "number of visits" information the next time he visits the page.
5. Cookie Dictionary
Sometimes many cookies variables may need to be defined in a page. In order to better manage it, the concept of one person "subkey" is often introduced in the cookies component. The syntax for quoting it is as follows:
Request.Cookies("Changename")("Subkey name")
For example, the following cookie creates a dictionary named "Dictionary", which saves three key values:
<%
Response.Cookie("info")("Myname")="jeff"
Response.Cookie("info")("Gender")="male"
Response.Cookie("info")("Myheight")="172"