Introduction to ASP's Cookie collection

Source: Internet
Author: User

Properties of Cookies:

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="11111"%>

If you do not specify a change attribute, the cookie disappears as the browser closes.

2, Domain properties

This property defines the unique domain that the cookie is to be routed. Such as: Cookies are only transferred to the site, you can use the following code.

<%Response.Cookies("domain").Domain="www.aspxuexi.com"%>

The syntax that an ASP uses to write cookies to the client is as follows:

Response.cookie ("Cookie name"). [(Key name]. property]= Content

Cookies are content that is contained in the HTTP header information, so

If an ASP file is to create a cookie, the following code should be placed before any HTML document to avoid an error.

<%Response.Cookies("CookieName")="NewCookie" %>
......

The ASP uses a collection of cookies from the request object 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 "您已是第" & Num & "次访问本站点了。"
else
Response.write "欢迎您首次访问本站。"
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.

Cookies child keys

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 cookie & "=" & Request.Cookies(cookie)
Else
for each key in Request.Cookies(cookie)
Response.write cookie&"("&key&")"&"="& Request.Cookies(cookie)(key)
next
end if
next
%>

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.