ASP Tutorial: Learning ASP Application Cookies skills

Source: Internet
Author: User
Tags date end key domain client

What is a Cookie? Cookies are actually a label that can often be heard in Chinese translation: small lick cakes. When you visit a Web site that needs to uniquely identify your site, it leaves a mark on your hard drive, and the next time you visit the same site, the site's page looks for the tag.

Each WEB site has its own tag, and the tagged content can be read at any time, but only by the page of that site. Cookies for each site exist within different files in the same folder as the cookie for all other sites (you can find them in the cookie folder in the Win98 Windows directory, and Win2K in the documents and Settings folder for specific In the user's Cookies folder).

A cookie is a token that uniquely identifies a customer, and a cookie can contain information that is shared by all pages of a WEB site during a conversation or several conversations, and can also be exchanged between pages using cookies. This feature is often used in ASP programs that require authentication of customer passwords as well as electronic bulletin boards, WEB chat rooms, and so on.

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 the use of cookies can be programmed to implement some bad intentions, most browsers have security settings, which can be set whether to allow or accept cookies (ie Browser "tools"-"Internet Options ..."-"Security"-"Custom Level"- -"Use of cookies", Netscape Browser "Tools"-"Cookie Manager"-"Manage stored cookies", 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.

How to use 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)

Basic syntax for creating cookies: response.cookies (cookies) [(key) |. Attribute]=value

The cookie here is the name of the specified cookie.

If a key is specified, the cookie is a dictionary.

(Test whether a cookie is a dictionary can be used in the following code to display a Boolean value: <%=request.cookies ("CookieName"). Haskeys%>. True is a dictionary, false is not. )

Attribute specifies information about the cookie itself. Attribute parameters can be one of the following:

If ①domain is specified, the cookie is sent to the request in the domain. The domain 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 you can also change it as needed. (Response.Cookies ("CookieName"). Domain = "www.webjx.com")

②path is a path property that enables more security requirements and limits the use of cookies by setting the exact path on the Web site. If this property is not set, the path to the application is used. (Response.Cookies ("CookieName"). Path = "/maindir/subdir/path")

③EXPIRES Specifies the expiration date for the cookie. In order to store cookies on the client disk after the session is over, or at many times, we want to save cookies on the visitor's computer for a long time. The date must be set. If the setting for this property does not exceed the current date, the cookie expires after the task ends.

Following code, you can set the expiration time of the cookie to "January 1, 2010": Response.Cookies ("CookieName"). expires= #January 01, 2010#

The following code will set the expiration time of the cookie to "the creation time of the cookie + 365 days": Response.Cookies ("CookieName"). expires=date+365

But it's best not to write Response.Cookies ("CookieName") casually. Expires=date, so that the call value between pages is empty.

Executing the following code will create a cookie on the visitor's computer with the name =visitorname and the value =ken:
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")

You can extend the following code to become the Cookie subkey key (Cookiesubname), the cookie dictionary generation. The code is as follows:

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

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

Basic syntax for reading cookies: request.cookies (cookies) [(key) |. Attribute

The cookie specifies the cookie whose value you want to retrieve.

The key optional parameter that retrieves the value of the subkey from the cookie dictionary.

ATTRIBE specifies information about the cookie itself. such as: HasKeys Read Only, specifies whether the cookie contains keywords.

If the client browser sends two cookies with the same name, then Request.cookie returns one of the deeper path structures. For example, if you have two cookies with the same name, but one of the path properties is/www/and the other is/www/home/, and the client browser sends two cookies to the/www/home/directory at the same time, Request.cookie will return only the second C Ookie.

Case study:

Num.asp (by recording the number of visits to the site within a year by a cookie left on the local disk, first display "first visit", and then "visits on")

The following are the referenced contents:

<%
Dim num
Num=request.cookies ("Visitnum")
If num > "0" Then
Num=num+1
Response.Write "You are already the first" &num& "time to visit this site. "
Else
Response.Write "You are welcome to visit this site for the first time. "
Num=1
End If
Response.Cookies ("Visitnum") =num
Response.Cookies ("Visitnum"). expires=date+365
%>

Showcookie.asp (from the Cookies folder to the browser all the cookie name, and the corresponding dictionary cookie display (blue Word display))

The following are the referenced contents:
<%
For all cookies in Request.Cookies
If Request.Cookies (cookie). HasKeys =false Then
Response.Write Cookies & "=" & Request.Cookies (Cookies)
Response.Write ("<br>")
Else
For each key in Request.Cookies (cookie)
Response.Write ("<font color=blue>")
Response.Write Cookies & ". ("&key&") "&" = "& Request.Cookies (Cookie) (key)
Response.Write ("</font><br>")
Next
End If
Next
%>

Check.asp

' First, set the page. Then, check the form variables (on the same page). If the form variable exists, the cookie is created and the expiration time is set.

The following are the referenced contents:
<%@ language= "VBSCRIPT"%>
<%
bgcolor = Request.Form ("bgcolor")
Fgcolor = Request.Form ("Fgcolor")
PWD = Request.Form ("pwd")
If bgcolor <> "" or Fgcolor <> "" Then
Response.Cookies ("Check") ("bgcolor") = bgcolor
Response.Cookies ("Check") ("fgcolor") = Fgcolor
Response.Cookies ("Check") ("pwd") = pwd
Response.Cookies ("Check"). expires= #may 01, 2004#
End If
' Next, read the cookie
bgcolor = Request.Cookies ("check") ("bgcolor")
Fgcolor = Request.Cookies ("check") ("Fgcolor")
PWD = Request.Cookies ("check") ("pwd")
' If the cookie does not exist on the visitor's computer, create a form and ask for relevant information
If bgcolor = "" and Fgcolor = "" and pwd= "" Then
%>
<HTML>
<HEAD> </HEAD>
<body>
<div align= "CENTER" >
<form action= "check.asp" method= "POST" >
bgcolor: <input type= "text" name= "bgcolor" ><BR>
Fgcolor: <input type= "text" name= "Fgcolor" ><BR>
Password:<input type= "Password" name= "pwd" ><BR>
<input type= "Submit" value= "Submit" >
</Form>
</DIV>
</BODY>
<%
End If
' If the cookie already exists and bgcolor exists, jump to color.asp.
If bgcolor <> "" Then
Response.Redirect "Color.asp"
End If
%>

Color.asp (Display the user's favorite color page characteristics)

The following are the referenced contents:

<%
Bgcolor=request.cookies ("Check") ("bgcolor")
Fgcolor=request.cookies ("Check") ("Fgcolor")
Pwd=request.cookies ("Check") ("pwd")
%>
<%response.write ("<body bgcolor=" &bgcolor& "><font color=" &fgcolor& ">")%>
Your password is:
<%response.write ("" &pwd& ")%>



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.