Guidelines for using Cookies in ASP

Source: Internet
Author: User
Tags variables domain
Cookies in fact, in web development, a cookie is just a text file, and when a user accesses a site, it is stored on the computer that the user is using, where the information is saved and the Web can extract the information when the user visits the site again later.

Although it sounds a little exciting now, you can actually use it to achieve a lot of meaningful functionality! For example, you can place a survey question and answer table on your site, ask your visitors favorite colors and fonts, and then follow these custom user Web interfaces. Also, you can save the visitor's login password so that when visitors visit the site again, they do not have to enter the password to log in again.

Of course, cookies also have some deficiencies. First, because cookies can be programmed to implement some bad intentions, most browsers have a security setting in which you can set whether cookies are allowed or accepted, so there is no guarantee that cookies can be used at any time. Furthermore, a visitor may intentionally or unintentionally delete a cookie. The original saved cookie will be lost when the visitor's machine encounters a "blue screen" panic, or reformatting the hard drive and installing the system. Finally, some of the most original browsers do not support cookies.

What can you do with Cooklie?

There are 2 basic ways of using cookies

1. Write cookies to the visitor's computer (using the response command)
2. Retrieve cookies from the visitor's computer (using the request command)



Response.Cookies ("CookieName") =value

Executing the following code will create a cookie, name =visitorname, and value =ken on the visitor's computer
Response.Cookies ("visitorname") = "Ken"

Executing the following code will create a cookie on the visitor's computer, the name =visitorname, the value = the value of the username in the form
Response.Cookies ("Visitorname") =request.form ("UserName")



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 ("Kenscookie")

Execute the following code to determine whether the cookie value named Kenscookie is "Yes":
Ifrequest.cookies ("Kenscookie") = "yes" then



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"

Before we explain the examples, we'll discuss 2 concepts: Command conventions and usage expiration times

  

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 property 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= ";

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"

  

Typically, a cookie does not exist when the browser is closed. But in many cases, such as the Web site example that will be discussed below, we hope to be more
Save cookies for a long time on the visitor's computer. Luckily, there is a way to achieve this. The following code allows you to set the expiration time of a cookie to 2010 years
January 1:
Response.Cookies ("CookieName"). Expires= #January01, 2010#

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, everyone needs to fill out the first visit, but after the same day to visit, there is no need for that
Do. With cookies, you can solve this problem very satisfactorily, and you don't need to use a database.



OK, let's start with the above code from the beginning.

First, the page is initially set and the cookie value named Kenssurvey is read:


<% @LANGUAGE = "VBSCRIPT"%>
<%
Survey=request.cookies ("Kenssurvey")
Then, determine if the cookie value already exists:

Ifsurvey= "" Then
If it does not exist, create and set a cookie and go to the page survey.asp. On the next visit, because of the existence of a cookie value, it is not transferred to the
Survey.asp page.

Response.Cookies ("Kenssurvey") = "X"
Response.Cookies ("Kenssurvey"). Expires= #January01, 2010#
Response.Redirect "Survey.asp"
If the cookie already exists, the visitor executes the remaining code in the page:

' Restofthepage

Endif
%>

Here's another simple example: When a visitor browses a site for the 1th time, show them the welcome message. The code is as follows:



Now let's look at what the code implementation is doing above. First, set the page. Then, check the form variables (on the same page). If the form variable is saved
In, the cookie is created and the expiration time is set.

<% @LANGUAGE = "VBSCRIPT"%>
<%
Requestname=request.form ("Name")
Requestleavemealone=request.form ("Leavemealone")
Ifrequestname<> "" Orrequestleavemealone<> "" Then
Response.Cookies ("Mysitevisitorname") =requestname
Response.Cookies ("Mysitevisitorname"). Expires= #January01, 2010#
Response.Cookies ("Mysiteleavemealone") =requestleavemealone
Response.Cookies ("Mysiteleavemealone"). Expires= #January01, 2010#
Endif

Next, read the cookie:

Visitorname=request.cookies ("Mysitevisitorname")
Leavemealone=request.cookies ("Mysiteleavemealone")
If the cookie does not exist on the visitor's computer, create a form that asks for relevant information:

Ifvisitorname= "" Andleavemealone= "" Then
%>
<HTML>
<HEAD>
</HEAD>
<bodybgcolor= "#ccffff" text= "Black" link= "Navy" vlink= "Purple" >
<divalign= "CENTER" >
<formaction= "index.asp" method= "POST" >
What ' Syourname (Leaveblankandhitthesubmitbuttonifyoudon ' Twantustoknow)?

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.