The most common method of cookies in ASP

Source: Internet
Author: User
Tags time limit

The most commonly used method of cookies in ASP, please take notes:

1. How to write cookies.

Response.Cookies ("field name") = variable or string, for example:

Response.Cookies ("name2") = "Dingdang"

2. How to set the cookie time.

Response.Cookies ("field name"). expires= time function +n, for example:

Response.Cookies ("Name2"). Expires=date+1, which means that Cookies are kept for 1 days, and then for example:

Response.Cookies ("Name2"). Expires=hour+8, which means Cookies are kept for 8 hours.

Response.Cookies ("Name2"). Expires=dateadd ("H", 24,now ())

3. In previous ASP tutorials, there are few ways to introduce cookies out of the way. You can write this in the "Exit" ASP page:

Response.Cookies ("field name") = ""

After that, the cookies are removed from the client's browser and the cookie file disappears. Notice how many fields you have, and how many sentences you need to write to clear.

4. How to read cookies.

Variable name =request.cookies ("field name"), for example:

Name2=request.cookies ("Name2")

If the page is written to <%=name2%>, "Dingdang" is displayed.

You can also read cookies,<%=request.cookies ("name2") directly in this way%>

Cookies are a part of the session object. However, cookies do not account for server resources, and "session" consumes server resources. So, try not to use the session, but use cookies.

First, let us understand the relevant knowledge of cookies

The pieces of information stored in cookies are stored in the form of a "name/value" pair (Name-value pairs), and a "name/value" pair is only a named piece of data. A Web site can only get information it puts on a user's computer, it cannot get information from other cookies, or anything else on the user's computer. Most of the content in cookies is encrypted, so the average user seems to be just a bunch of meaningless alphanumeric combinations, and only the Web page programs (client or server-side programs) of the Web site know what they really mean.

Cookies stored in the user's computer hard disk can be read by the Web browser, which is named in the username @ web address [digital].txt.

Second, the collection of cookies has the following several attributes:

1.Expires attribute: This property is used to set a time limit for cookies that can be invoked as long as the Web page is opened so that cookies are automatically deleted when the time expires. If a cookie does not have an expiration date, its life cycle starts with the open browser, the end of the browser closes, and the life cycle ends after each run, and the next run starts again.

2.Domain attribute: This property defines the uniqueness of the cookie's data transfer, specifying the domain of the data transfer, not the other domain.

3.Path attribute: The cookie is defined to send only the specified path request, and if the path attribute is not set, the default path is used.

4.Secure Property: Specifies whether the cookie is transmitted using encryption (SSL).

5.Haskeys property: If the requested cookie is a cookie dictionary with multiple key values, returns True, which is a read-only property.

Third, the basic operation of cookies

The operation of cookies boils down to two types: write and read

Use response to write in ASP, read with request. For example:

1. Write cookies

Response.Cookies ("Cookiesname") =value

2. Read cookies

Request.Cookies ("Cookiesname")

Four, the extended application of cookies

1. Read and write the key values of cookies (cookiesubname), examples are as follows:

Response.Cookies ("Visitorname") ("FirstName") = "Martin"

Response.Cookies ("Visitorname") ("LastName") = "Brawn"

Firstname=request.cookies ("Visitorname") ("FirstName")

Lastname=request.cookies (visitorname ") (" LastName ")

2. Define the domain properties of cookies (domain): Field properties indicate which Web site The cookie is generated from or read, and by default, the domain property of the cookie is set to the Web site that generated it, but it can also be changed as needed. such as: Response.Cookies ("CookieName"). Domain = "Www.yourdomain.com"

3. Define the path attribute (path) of cookies: path attributes can achieve more security requirements by setting the exact path on the Web site to limit the use of cookies. For example:

Response.Cookies ("CookieName"). Path = "/maindir/subdir/path"

4. Set the expiration time of cookies

If you do not specify an expiration time, the cookie is automatically deleted when the browser is closed. If the expiration time is specified, the cookie will normally be deleted after the expiration date (not a computer failure or manually deleted by the user). If the set expiration time is less than the current time, the same processing is not set due time.

For example: Response.Cookies ("UserName"). expires= #October 01, 2005# namely: October 1, 2005 expired

Response.Cookies ("UserName") =now () +1 namely: tomorrow at this time expires (save one day)

Secure property for 5.Cookies: The value of the secure property is only true or false two. If the cookie data is specified as Secure=true, the data is submitted as SSL (Secure Sockets Layer): Response.Cookies ("UserName"). Secure=true

V. Determine if the user's browser has cookies open

Write the code written to the cookie on the page requested by the user, and then read the cookie, not empty, and the cookie is turned on, and the other is not turned on. The session is not supported by browsers that do not have cookies open.

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.