Asp: Cookies application guide, detailed code and tutorials
Author:Eve Cole
Update Time:2009-06-24 17:28:30
In fact, in web development, a cookie is just a text file. When a user visits a site, it is stored on the computer used by the user. Some information is saved in it. When the user visits the site again in the future, the web can Extract this information.
Although cookies may not sound exciting right now, you can actually do a lot of meaningful things with them! For example: you can place a survey on your site, asking visitors for their favorite colors and fonts, and then customize the user's web interface based on these. Moreover, you can also save the visitor's login password, so that when the visitor visits the site again, he or she does not have to enter a password to log in.
Of course, cookies also have some shortcomings. First of all, since the function of cookies can be programmed to achieve some bad intentions, most browsers have security settings in which you can set whether to allow or accept cookies, so there is no guarantee that cookies can be used at any time. Furthermore, visitors may intentionally or unintentionally delete
cookies. When the visitor's machine encounters a "blue screen" of crash, or after reformatting the hard disk or installing the system, all previously saved cookies will be lost. Finally, some of the earliest browsers do not support cookies.
What can you do with cookies?
There are 2 basic ways to use cookies:
1. Write cookies to the visitor's computer (use the RESPONSE command)
2. Retrieve cookies from the visitor's computer (use the REQUEST command)
Basic syntax for creating cookies
Response.Cookies("CookieName")=value
Executing the following code will create a cookie on the visitor's computer with name=VisitorName and value=Ken
Response.Cookies("VisitorName")="Ken"
Executing the following code will create a cookie on the visitor's computer, with name = VisitorName and value = the value of UserName in the form.
Response.Cookies("VisitorName")=Request.Form("UserName")
Basic syntax for reading cookies
Request.Cookies("CookieName")
The Request value can be treated as a variable. Executing the following code will retrieve the cookie value named KensCookie and store it in the variable MyVar:
MyVar=Request.Cookies("KensCookie")
Executing the following code will determine whether the cookie value named KensCookie is "Yes":
If Request.Cookies("KensCookie")="Yes" then
Feature-rich cookies
You can extend the above code to become the Cookie sub key value (CookieSubName), the code is as follows:
Response.Cookies("VisitorName")("FirstName")="Ken"
Response.Cookies("VisitorName")("LastName")="Baumbach"
Before explaining the examples, let’s discuss two final concepts: command convention and usage expiration time.
naming convention
Like naming other variables, naming cookies appropriately and uniquely will help you use it consistently in your program. You can use 1 or 2 of the following
The cookie attribute names the cookie variable:
Domain attribute (Domain): The domain attribute indicates which website the cookie is generated or read from. By default, the domain attribute of the cookie is set to the website that generated it, but you can also change it as needed. The relevant code is as follows: Response.Cookies("CookieName").Domain = " www.mydomain.com "
Path attribute (Path): The path attribute can implement more security requirements. By setting the precise path on the website, you can limit the scope of cookie use. For example:
Response.Cookies("CookieName").Path = "/maindir/subdir/path"
Use expiration time
Normally, a cookie ceases to exist when the browser is closed. But many times, such as the web site example discussed below, we want to save cookies on the visitor's computer for a longer period of time. Fortunately, there is a way to achieve this. The following code can set the cookie expiration time to 2010
January 1st:
Response.Cookies("CookieName").Expires=#January 01, 2010#
Executing the following code will set the cookie expiration time to "cookie creation time + 365 days":
Response.Cookies("CookieName")=Date+365
Practical examples of using cookies (very exciting)
Now let’s talk about practical examples. Suppose: You want to do a survey. Everyone needs to fill in the information when they visit for the first time, but when they visit again in the future, they don’t need to do that again. Using cookies, you can solve this problem very satisfactorily without using a database.
< %@ LANGUAGE="VBSCRIPT" % >
<%
Survey=Request.Cookies("KensSurvey")
If Survey="" then
Response.Cookies("KensSurvey")="x"
Response.Cookies("KensSurvey").Expires=#January 01, 2010#
Response.Redirect "survey.asp"
Else
'rest of the page
End if
%>
Okay, let’s start from scratch and discuss the above code.
First, initially set up the page and read the cookie value named KensSurvey:
< %@ LANGUAGE="VBSCRIPT" % >
<%
Survey=Request.Cookies("KensSurvey")
Then, determine whether the cookie value already exists:
If Survey="" then
If it does not exist, the cookie is created and set, and the page is redirected to survey.asp. When you visit next time, because the cookie value exists, you will not go to
survey.asp page.
Response.Cookies("KensSurvey")="x"
Response.Cookies("KensSurvey").Expires=#January 01, 2010#
Response.Redirect "survey.asp"
If the cookie already exists, the visitor will execute the remaining code on the page:
'rest of the page
End if
%>
Example 2
Here's another simple example: Show a welcome message to visitors when they visit a site for the first time. The code is as follows:
< %@ LANGUAGE="VBSCRIPT" % >
<%
RequestName = Request.Form("Name")
RequestLeaveMeAlone = Request.Form("LeaveMeAlone")
If RequestName < >"" or RequestLeaveMeAlone < >"" then
Response.Cookies("MySiteVisitorName") = RequestName
Response.Cookies("MySiteVisitorName").Expires = #January 01, 2010#
Response.Cookies("MySiteLeaveMeAlone") = RequestLeaveMeAlone
Response.Cookies("MySiteLeaveMeAlone").Expires = #January 01, 2010#
End if
VisitorName = request.cookies("MySiteVisitorName")
LeaveMeAlone = request.cookies("MySiteLeaveMeAlone")
If VisitorName ="" and LeaveMeAlone ="" then
%>
< HTML > < HEAD > < /HEAD >
< body bgcolor="#ccffff" text="black" link="navy" vlink="purple" >
< DIV ALIGN="CENTER" >
< form action="index.asp" method="POST" >
< H2 >Let's be friends< /H2 >
What's your name (leave blank and hit the Submit button if you don't want us to
know)?
< input type="text" name="name" >< BR >< BR >
< input type="hidden" name="LeaveMeAlone" value="x" >
< input type="submit" value="Submit" >
</ /FORM >
< /DIV >
< /BODY >
<%
End if
If VisitorName < > "" then
Response.write "Hi, " & VisitorName & "! I hope you are having a great day!"
End if
'rest of the page
%>
Okay, now let’s take a look at what the above code implementation does. First, set up the page. Then, check the form variable (in the same page). If the form variable exists, create the cookie and set the expiration time.
< %@ LANGUAGE="VBSCRIPT" % >
<%
RequestName = Request.Form("Name")
RequestLeaveMeAlone = Request.Form("LeaveMeAlone")
If RequestName < >"" or RequestLeaveMeAlone < >"" then
Response.Cookies("MySiteVisitorName") = RequestName
Response.Cookies("MySiteVisitorName").Expires = #January 01, 2010#
Response.Cookies("MySiteLeaveMeAlone") = RequestLeaveMeAlone
Response.Cookies("MySiteLeaveMeAlone").Expires = #January 01, 2010#
End if
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 asking for the relevant information:
If VisitorName ="" and LeaveMeAlone ="" then
%>
<HTML>
<HEAD>
< /HEAD >
< body bgcolor="#ccffff" text="black" link="navy" vlink="purple" >
< DIV ALIGN="CENTER" >
< form action="index.asp" method="POST" >
< H2 >Let's be friends< /H2 >
What's your name (leave blank and hit the Submit button if you don't want us to
know)?
< input type="text" name="name" >< br >< br >
< input type="hidden" name="LeaveMeAlone" value="x" >
< input type="submit" value="Submit" >
</ /FORM >
< /DIV >
< /BODY >
<%
End if
If the cookie already exists and the username exists, display a welcome screen to the visitor and execute the rest of the code.
If VisitorName < > "" then
Response.write "Hi, " & VisitorName & "! I hope you are having a great day!"
End if
'rest of the page
%>
Although the above example is simple, many creative applications can be extended from it. You can add many features to your forms to customize your web site.
You can also let visitors customize your site's colors, fonts, and other web elements. If possible, you can ask the visitor for their birthday and when the visitor visits on that day, you can display a "Happy Birthday" message to him.
As you can see, the scalability of cookies is endless, and this article is just a starting point.