The basic use method of cookies in ASP

Source: Internet
Author: User

There are 2 basic ways to use cookies set:

1. Write cookies to the visitor's computer (RESPONSE)

2, retrieve the cookie from the visitor's computer (REQUEST)

If you are on how cookies in the browser and between the server to pass, you can refer to the original site some of the articles.

For example: http://www.aspxuexi.com/aspbasic/responseandrequest/2006-4-6/asp_reponse_request.htm

Understanding the relationship between HTTP headers and cookies is helpful for your programming. At least you will not try to read cookies on this page on the same page.

A method for creating the basic syntax response object for a cookie.

Response.Cookies("CookieName")=“aspxuexi.com”

Executing the following code will create a cookie on the visitor's computer, named =visitorname, value = String aspxuexi.com

Response.Cookies("VisitorName")=Request.Form("UserName")

A method of reading the basic syntax request object for a cookie.

   Request.Cookies("CookieName")

You can treat the request value as a variable, execute the following code, retrieve the cookie value named Kenscookie and deposit it in the variable myvar:

MyVar=Request.Cookies("CookieName")  ‘MyVar="aspxuexi.com"

Feature-Rich cookies

You can extend the above code to become a cookie subkey key value (Cookiesubname), the code is as follows:

Response.Cookies("VisitorName")("FirstName")="Ken"
Response.Cookies("VisitorName")("LastName")="Baumbach"

Cookie Command Convention and use expiration time.

Naming conventions

As with other variables, naming cookies appropriately and uniquely helps to use it consistently in your program. You can use the following 1 or 2 cookie properties to name the cookie variable:

Domain Properties: Field properties indicate which Web site The cookie is generated from or read, by default, the domain property of the cookie is set to produce its web site, but you

You can also change it as needed. The relevant code is as follows: Response.Cookies ("CookieName"). Domain = "aspxuexi.com" ' specifies that the domain attribute of the cookie is aspxuexi.com this value and www.aspxuexi.com are completely different

Path Properties (PATH): Path properties 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" ' another directory of the Web site will not be able to read this value

Use Expiration time

Typically, a cookie does not exist when the browser is turned off, which is said to be by default. But in many cases, like the Web site example that will be discussed below, we want to save cookies for a longer time on the visitor's computer. Luckily, there is a way to achieve this. The following code allows you to set the use expiration date for a cookie to January 1, 2010:

Response.Cookies ("CookieName"). expires= #January 01, 2010# ' Here you should note the date format in VBScript

Execute the following code to set the expiration time of the cookie to "+365 days of the cookie creation time":

Response.Cookies("CookieName")=Date+365

Practical examples of using cookies

Now let's discuss the actual example. Suppose you want to do a survey, each person needs to fill out the information when they first visit, but you don't need to do that again when you visit later in the day. With cookies, you can solve this problem very satisfactorily, and you don't need to use a database.

< %
Survey=Request.Cookies("KensSurvey")
If Survey ="" then
Response.Cookies("KensSurvey")="x"
Response.Cookies("KensSurvey").Expires=#January 01, 2010#
Response.Redirect "survey.asp"
Else
&apos;rest of the page
End if
% >

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.