2025-04-27 07:49:33 -04:00

1060 lines
46 KiB
HTML

<!DOCtype HTML PUBLIC "-//W3C//Dtd HTML 3.2//EN">
<html dir=ltr><head><title>Module 3: Maintaining Session State in a Web Application</title>
<SCRIPT LANGUAGE="JavaScript">
<!--
TempString = navigator.appVersion
if (navigator.appName == "Microsoft Internet Explorer"){
// Check to see if browser is Microsoft
if (TempString.indexOf ("4.") >= 0){
// Check to see if it is IE 4
document.writeln('<link rel="stylesheet" type="text/css" href="/iishelp/common/coua.css">');
}
else {
document.writeln('<link rel="stylesheet" type="text/css" href="/iishelp/common/cocss.css">');
}
}
else if (navigator.appName == "Netscape") {
// Check to see if browser is Netscape
document.writeln('<link rel="stylesheet" type="text/css" href="/iishelp/common/coua.css">');
}
else
document.writeln('<link rel="stylesheet" type="text/css" href="/iishelp/common/cocss.css">');
//-->
</script>
<SCRIPT LANGUAGE="VBScript">
<!--
Sub Window_OnLoad()
Dim frmContents
On Error Resume Next
If Not Parent Is Nothing Then
Set frmContents = Parent.Contents
If Not frmContents Is Nothing Then
frmContents.Window.TOCSynch_Click
End If
End If
End Sub
//-->
</SCRIPT>
<META NAME="ROBOTS" CONTENT="NOINDEX"><META HTTP-EQUIV="Content-Type" content="text/html; charset=Windows-1252"></head>
<body bgcolor="#FFFFFF" text="#000000"><font face="Verdana,Arial,Helvetica">
<h1><a name="maintainingsessionstate">Module 3: Maintaining Session State</a></h1>
<p>
This module describes the process of maintaining session state in Active
Server Pages (ASP). Session refers to the time segment that
a specific user is actively viewing the contents of a Web site. A session
starts when the user visits the first page on the site, and it ends a few minutes
after the user leaves the site. The pieces of user-specific information, relevant
to a particular session, are collectively known as <i>session state</i>.
</p>
<p>Because HTTP is a stateless protocol, a problem arises when trying to
maintain state for a user visiting your Web site. The Web server treats each
HTTP request as a unique request, which is unrelated to any previous requests.
Thus, the information a user enters on one page (through a form, for example)
is not automatically available on the next page requested. The Web server must
maintain session state to identify and track users as they browse through pages
on a Web site.
</p>
<p>One solution is through the use of <i>cookies</i>. Cookies record information
about a user on one page and transfer that information to other pages within
the site. However, a few browsers do not recognize cookies, and on other browsers,
users can disable cookies. If you are concerned about reaching this Web audience,
you can maintain session state without using cookies by using HTTP POST.
</p>
<p>
This module includes the following lessons:
</p>
<ul>
<li>
<a href="#with">Maintaining Session State with Cookies</a>&nbsp;&nbsp;&nbsp;Provides two
cookie examples, one using the ASP Response and Request objects and another
using the ASP Session object.
</li><li>
<a href="#without">Maintaining Session State Without Cookies</a>&nbsp;&nbsp;&nbsp;Provides
an example of the alternative to maintaining session state with cookies: HTTP POST.
</li>
</ul>
<BR><h2><a name="with">Maintaining Session State with Cookies</a></h2>
<p>
Cookies store a set of user specific information, such as a credit card number
or a password. The Web server embeds the cookie into a user's Web browser so that the user's information becomes available to other pages within the site; users do
not have to reenter their information for every page they visit. Cookies are a
good way to gather customer information for Web-based shopping, for retaining the
personal preferences of the Web user, or for maintaining state about the user.
</p>
<p>
There are two kinds of cookies, as follows:
</p>
<ul>
<li>
<B>In-memory cookies</b>&nbsp;&nbsp;&nbsp;An in-memory cookie goes away when the user shuts the browser down.
</li><li>
<B>Persistent cookies</b>&nbsp;&nbsp;&nbsp;A persistent cookie resides on the hard drive of the user and is retrieved when the user comes back to the Web page.
</li>
</ul>
<p>
If you create a cookie without specifying an expiration date, you are creating
an in-memory cookie, which lives for that browser session only. The following
illustrates the script that would be used for an in-memory cookie:
</p>
<code>
<p>
&nbsp;&nbsp;&nbsp;Response.Cookies("SiteArea") = "TechNet"
</p>
</code>
<p>
If you want the cookie information to persist beyond the session, you should
create a persistent cookie by specifying an expiration date. Supplying an
expiration date causes the browser to save the cookie on the client computer.
Until the cookie expiration date is reached, the data in the persistent cookie
will stay on the client machine. Any request to the original Web site will
automatically attach the cookie that was created by that site. Cookies go only to the sites that created them because part of the Web site name and ASP file
make up the data in the cookie.
<BR>The following illustrates the script used to create a persistent cookie:
</p>
<code>
<p>
&nbsp;&nbsp;&nbsp;Response.Cookies("SiteArea") = "TechNet"
<br>&nbsp;&nbsp;&nbsp;Response.Cookies("SiteArea").Expires = "August 15, 2000"
</p>
</code>
<p>The script to create a cookie should be placed at the beginning of the ASP
file because cookies need to be generated before any HTML text is sent to the
browser.
</p>
<BR><h3>Cookies Using the Response and Request Objects</h3>
<p>
Persistent cookies are produced using the <b>Response</b> and <b>Request</b> objects,
although these objects may also be used to create an in-memory cookie. The
majority of Web applications employ these objects to maintain session state.
</p>
<ul>
<li>
<B>Response object</B>&nbsp;&nbsp;&nbsp;Use the <b>Response</b> object to create and set cookie values.
</li><li>
<B>Request object</B>&nbsp;&nbsp;&nbsp;Use the <b>Req</b>uest object to retrieve the value of a cookie
created during a previous Web session.
</li>
</ul>
<p>
In this lesson you will use the <b>Response</b> and <b>Request</b> objects to create the following
files. Please create them all at once, because some of them need the others. After you
have created all the files, run the application by typing
<b>http&#58;&#47;&#47;LocalHost&#47;Tutorial&#47;Frame.htm</b> in your browser.
</p>
<ul>
<li>
<B>Frame.htm</B>&nbsp;&nbsp;&nbsp;A page that splits the the user's view into two windows. This
page requires that Menu.htm and CustomGreeting.asp.
</li><li>
<B>Menu.htm</B>&nbsp;&nbsp;&nbsp;A page containing links to the samples for this lesson. For the links to work, this
page requires that all the other pages have been created.
</li><li>
<B>CustomGreeting.asp</B>&nbsp;&nbsp;&nbsp;An ASP script that takes the user's name in a form
and sets an in-memory cookie.
</li><li>
<B>DeleteGreetingCookie.asp</B>&nbsp;&nbsp;&nbsp;An ASP script that deletes the cookie that
contains the user's name. If no cookie is set, a warning is displayed.
</li><li>
<B>SelectColors.asp</B>&nbsp;&nbsp;&nbsp;An ASP script that sets up the cookies for the user's
color choices.
</li><li>
<B>DeleteColorCookie.asp</B>&nbsp;&nbsp;&nbsp;An ASP script that deletes the Web colors previously
chosen. If none are chosen, a warning is displayed.
</li><li>
<B>Cookie.asp</B>&nbsp;&nbsp;&nbsp;An ASP script that sets persistent cookies to hold the current
date and time of the user's visit and record the total number of visits.
</li><li>
<B>DeleteCookies.asp</B>&nbsp;&nbsp;&nbsp;This ASP script deletes the cookies set in Cookie.asp. If no
cookies are set, a warning is displayed.
</li></ul>
<BR><h4>Frame.htm</h4>
<p>
Open a new file in your text editor, paste in the following script, and save
the file as <b>C:\Inetpub\Wwwroot\Tutorial\Frame.htm</b>.
</p>
<code>
<p>
&nbsp; &lt;html&gt;
<BR>&nbsp; &lt;head&gt;
<BR>&nbsp; &lt;title&gt;Customized Greeting and Colors Using In-Memory and Persistent Cookies&lt;/title&gt;
<BR>&nbsp; &lt;/head&gt;
<BR>
<BR>&nbsp; &lt;frameset cols="40%,60%"&gt;
<BR>&nbsp;&nbsp;&nbsp; &lt;frame src="menu.htm" name="left" marginheight="5" marginwidth="5"&gt;
<BR>&nbsp;&nbsp;&nbsp; &lt;frame src="CustomGreeting.asp" name="right" marginheight="5" marginwidth="5"&gt;
<BR>&nbsp; &lt;/frameset&gt;
<BR>
<BR>&nbsp; &lt;noframes&gt;
<BR>&nbsp;&nbsp;&nbsp; Sorry, your browser does not support frames. Please go to the &lt;a href="menu.htm"&gt;Menu&lt;/a&gt;.
<BR>&nbsp; &lt;/noframes&gt;
<BR>
<BR>&nbsp; &lt;/html&gt;
</p>
</code>
<BR><h4>Menu.htm</h4>
<p>
Open a new file in your text editor, paste in the following script, and save
the file as <b>C:\Inetpub\Wwwroot\Tutorial\Menu.htm</b>.
</p>
<code>
<p>
&nbsp; &lt;html&gt;
<BR>&nbsp; &lt;head&gt;
<BR>&nbsp; &lt;title&gt;Maintaining Session State With Cookies&lt;/title&gt;
<BR>&nbsp; &lt;/head&gt;
<BR>&nbsp; &lt;body&gt;
<BR>&nbsp; &lt;font face="MS Gothic"&gt;
<BR>
<BR>&nbsp; &lt;h2 align="center"&gt;Cookie Examples&lt;/h2&gt;
<BR>
<BR>&nbsp; &lt;table align=center border=1 cellpadding=4&gt;
<BR>&nbsp;&nbsp;&nbsp; &lt;tr&gt;
<BR>&nbsp;&nbsp;&nbsp; &lt;td&gt;&lt;a href="CustomGreeting.asp" target="right"&gt;&lt;b&gt;Custom Greeting Page&lt;/b&gt;&lt;/a&gt;&lt;/td&gt;
<BR>&nbsp;&nbsp;&nbsp; &lt;/tr&gt;&lt;tr&gt;
<BR>&nbsp;&nbsp;&nbsp; &lt;td&gt;&lt;a href="DeleteGreetingCookie.asp" target="right"&gt;&lt;b&gt;Delete the Greetings Cookie&lt;/b&gt;&lt;/a&gt;&lt;/td&gt;
<BR>&nbsp;&nbsp;&nbsp; &lt;/tr&gt;&lt;tr&gt;
<BR>&nbsp;&nbsp;&nbsp; &lt;td&gt;&lt;a href="SelectColors.asp" target="right"&gt;&lt;b&gt;Set Page Colors&lt;/b&gt;&lt;/a&gt;&lt;/td&gt;
<BR>&nbsp;&nbsp;&nbsp; &lt;/tr&gt;&lt;tr&gt;
<BR>&nbsp;&nbsp;&nbsp; &lt;td&gt;&lt;a href="DeleteColorCookie.asp" target="right"&gt;&lt;b&gt;Delete Page Colors Cookies&lt;/b&gt;&lt;/a&gt;&lt;/td&gt;
<BR>&nbsp;&nbsp;&nbsp; &lt;/tr&gt;&lt;tr&gt;
<BR>&nbsp;&nbsp;&nbsp; &lt;td&gt;&lt;a href="Cookie.asp" target="right"&gt;&lt;b&gt;Set Cookies for Date, Time and Total Visits&lt;/b&gt;&lt;/a&gt;&lt;/td&gt;
<BR>&nbsp;&nbsp;&nbsp; &lt;/tr&gt;&lt;tr&gt;
<BR>&nbsp;&nbsp;&nbsp; &lt;td&gt;&lt;a href="DeleteCookies.asp" target="right"&gt;&lt;b&gt;Delete Cookies for Date, Time and Total Visits&lt;/b&gt;&lt;/a&gt;&lt;/td&gt;
<BR>&nbsp;&nbsp;&nbsp; &lt;/tr&gt;
<BR>&nbsp; &lt;/table&gt;
<BR>
<BR>&nbsp; &lt;/font&gt;
<BR>&nbsp; &lt;/body&gt;
<BR>&nbsp; &lt;/html&gt;
</p>
</code>
<BR><h4>CustomGreeting.asp</h4>
<p>
Open a new file in your text editor, paste in the following script, and save
the file as <b>C:\Inetpub\Wwwroot\Tutorial\CustomGreeting.asp</b>.
</p>
<code>
<p>
&nbsp; &lt;%@ Language="VBScript" %&gt;
<BR>&nbsp; &lt;%
<BR>&nbsp;&nbsp; 'If the user has selected text and background colors,
<BR>&nbsp;&nbsp; ' cookies are used to remember the values between HTTP sessions.
<BR>&nbsp;&nbsp; 'Do this first so that your page can use use the values if they are set.
<BR>&nbsp;&nbsp; If Not (Request.QueryString("Text")="") Then
<BR>&nbsp;&nbsp;&nbsp;&nbsp; Response.Cookies("TextColor") = Request.QueryString("Text")
<BR>&nbsp;&nbsp;&nbsp;&nbsp; Response.Cookies("BackgroundColor") = Request.QueryString("Background")
<BR>&nbsp;&nbsp; End If
<BR>
<BR>&nbsp;&nbsp; ' If the user has typed in a name, a cookie is created.
<BR>&nbsp;&nbsp; If Not (Request.QueryString("Name")="") Then
<BR>&nbsp;&nbsp;&nbsp;&nbsp; Response.Cookies ("Name") = Request.QueryString("Name")
<BR>
<BR>&nbsp;&nbsp; ' If the user does not give his/her name, a cookie
<BR>&nbsp;&nbsp; ' is created so that we do not keep asking for the name.
<BR>&nbsp;&nbsp; ElseIf (InStr(Request.QueryString,"Name")=1) Then
<BR>&nbsp;&nbsp;&nbsp;&nbsp; Response.Cookies ("NoUserInput") = "TRUE"
<BR>
<BR>&nbsp;&nbsp; End If
<BR>&nbsp; %&gt;
<BR>
<BR>&nbsp; &lt;html&gt;
<BR>&nbsp; &lt;head&gt;
<BR>&nbsp; &lt;/head&gt;
<BR>
<BR>&nbsp; &lt;%
<BR>&nbsp;&nbsp; 'Set colors according to existing previous user input.
<BR>&nbsp;&nbsp; If (Request.Cookies ("TextColor")="") Then %&gt;
<BR>&nbsp;&nbsp;&nbsp;&nbsp; &lt;body&gt;
<BR>&nbsp;&nbsp; &lt;% Else %&gt;
<BR>&nbsp;&nbsp;&nbsp;&nbsp; &lt;body bgcolor=&lt;%=Request.Cookies("BackgroundColor")%&gt; text=&lt;%=Request.Cookies("TextColor")%&gt;&gt;
<BR>&nbsp;&nbsp; &lt;% End If
<BR>&nbsp; %&gt;
<BR>
<BR>&nbsp; &lt;font face="MS Gothic"&gt;
<BR>
<BR>&nbsp; &lt;%
<BR>&nbsp;&nbsp; 'If there is no name cookie set, no name entered by the user,
<BR>&nbsp;&nbsp; ' and there was no user input at all, get the user's name.
<BR>&nbsp;&nbsp; If ( (Request.Cookies("Name")="") And ((Request.QueryString("Name"))="")) And (Not(Request.Cookies("NoUserInput")="TRUE") ) Then %&gt;
<BR>
<BR>&nbsp;&nbsp;&nbsp;&nbsp; &lt;FORM ACTION="CustomGreeting.asp" METHOD="GET" NAME="DataForm"&gt;
<BR>&nbsp;&nbsp;&nbsp;&nbsp; &lt;table align=center&gt;&lt;tr&gt;&lt;td&gt;
<BR>&nbsp;&nbsp;&nbsp;&nbsp; &lt;INPUT TYPE=TEXTBOX NAME="Name" SIZE=33&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;
<BR>&nbsp;&nbsp;&nbsp;&nbsp; &lt;INPUT TYPE=Submit VALUE="Please Enter Your Name"&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
<BR>&nbsp;&nbsp;&nbsp;&nbsp; &lt;/FORM&gt;
<BR>
<BR>&nbsp;&nbsp; &lt;% ElseIf Not(Request.Cookies("Name")="") Then %&gt;
<BR>
<BR>&nbsp;&nbsp;&nbsp;&nbsp; &lt;H2 align=center&gt;Greetings &lt;%=Request.Cookies("Name")%&gt;&lt;/H2&gt;
<BR>
<BR>&nbsp;&nbsp; &lt;% Else %&gt;
<BR>
<BR>&nbsp;&nbsp;&nbsp;&nbsp; &lt;H2&gt;Hello!&lt;/H2&gt;
<BR>&nbsp;&nbsp;&nbsp;&nbsp; &lt;H3&gt;You did not give us your name so we are not able to greet you by name.&lt;/H3&gt;
<BR>
<BR>&nbsp;&nbsp; &lt;% End If
<BR>&nbsp; %&gt;
<BR>
<BR>&nbsp; &lt;H3&gt;In-Memory Cookie Example&lt;/H3&gt;
<BR>&nbsp; &lt;P&gt;
<BR>&nbsp; Once you enter your name:
<BR>&nbsp; &lt;UL&gt;
<BR>&nbsp; &lt;LI&gt;If you hit &lt;B&gt;Refresh&lt;/B&gt; in your browser, you should still see your name.&lt;/LI&gt;
<BR>&nbsp; &lt;LI&gt;If you close your browser, the cookie is deleted. When you re-open your browser to this page, you should be asked for your name again.&lt;/LI&gt;
<BR>&nbsp; &lt;LI&gt;If you click on &lt;B&gt;Delete the Greetings Cookie&lt;/B&gt;, and click on &lt;B&gt;Custom Greeting Page&lt;/B&gt;, you should be asked for your name again.&lt;/LI&gt;
<BR>&nbsp; &lt;/P&gt;
<BR>
<BR>&nbsp; &lt;/font&gt;
<BR>&nbsp; &lt;/body&gt;
<BR>&nbsp; &lt;/html&gt;
</p>
</code>
<BR><h4>DeleteGreetingCookie.asp</h4>
<p>
Open a new file in your text editor, paste in the following script, and save
the file as <B>C:\Inetpub\Wwwroot\Tutorial\DeleteGreetingCookie.asp</b>.
</p>
<code>
<p>
&nbsp; &lt;%@ Language="VBScript" %&gt;
<BR>
<BR>&nbsp; &lt;html&gt;
<BR>&nbsp; &lt;head&gt;
<BR>&nbsp; &lt;/head&gt;
<BR>
<BR>&nbsp; &lt;% If (Request.Cookies ("TextColor")="") Then %&gt;
<BR>&nbsp;&nbsp;&nbsp;&nbsp; &lt;body&gt;
<BR>&nbsp;&nbsp;&nbsp;&nbsp; &lt;font face="MS Gothic"&gt;
<BR>&nbsp; &lt;% Else %&gt;
<BR>&nbsp;&nbsp;&nbsp;&nbsp; &lt;body bgcolor=&lt;%=Request.Cookies("BackgroundColor")%&gt; text=&lt;%=Request.Cookies("TextColor")%&gt;&gt;
<BR>&nbsp;&nbsp;&nbsp;&nbsp; &lt;font face="MS Gothic" color=&lt;%=Request.Cookies("TextColor")%&gt;&gt;
<BR>&nbsp; &lt;% End If %&gt;
<BR>
<BR>&nbsp; &lt;%
<BR>&nbsp;&nbsp; If Not ("" = Request.Cookies("Name")) Then
<BR>&nbsp;&nbsp;&nbsp;&nbsp; Response.Cookies ("Name").Expires = "January 1, 1992"
<BR>&nbsp;&nbsp;&nbsp;&nbsp; Response.Cookies ("NoUserInput").Expires = "January 1, 1992" %&gt;
<BR>
<BR>&nbsp;&nbsp;&nbsp;&nbsp; &lt;h2 align=center&gt;In-Memory Greeting Cookie Deleted&lt;/h2&gt;
<BR>&nbsp;&nbsp;&nbsp;&nbsp; &lt;P&gt;
<BR>&nbsp;&nbsp;&nbsp;&nbsp; The cookie used to keep track of your name has been deleted.&lt;BR&gt;
<BR>&nbsp;&nbsp;&nbsp;&nbsp; Please click on &lt;B&gt;Custom Greeting Page&lt;/B&gt; to be asked for your name again.
<BR>&nbsp;&nbsp;&nbsp;&nbsp; &lt;/P&gt;
<BR>
<BR>&nbsp;&nbsp; &lt;% Else %&gt;
<BR>
<BR>&nbsp;&nbsp;&nbsp;&nbsp; &lt;h2 align=center&gt;No In-Memory Greeting Cookie Deleted&lt;/h2&gt;
<BR>&nbsp;&nbsp;&nbsp;&nbsp; &lt;P&gt;
<BR>&nbsp;&nbsp;&nbsp;&nbsp; There was no cookie set with your name.&lt;BR&gt;
<BR>&nbsp;&nbsp;&nbsp;&nbsp; Please click on &lt;B&gt;Custom Greeting Page&lt;/B&gt; to enter your name.
<BR>&nbsp;&nbsp;&nbsp;&nbsp; &lt;/P&gt;
<BR>
<BR>&nbsp;&nbsp; &lt;% End If
<BR>&nbsp; %&gt;
<BR>
<BR>&nbsp; &lt;/font&gt;
<BR>&nbsp; &lt;/body&gt;
<BR>&nbsp; &lt;/html&gt;
</p>
</code>
<br><h4>SelectColors.asp</h4>
<p>
Open a new file in your text editor, paste in the following script, and save
the file as <B>C:\Inetpub\Wwwroot\Tutorial\SelectColors.asp</b>.
</p>
<code>
<p>
&nbsp; &lt;%@ Language="VBScript" %&gt;
<BR>
<BR>&nbsp; &lt;%
<BR>&nbsp;&nbsp;&nbsp; ' If the user has selected text and background colors,
<BR>&nbsp;&nbsp;&nbsp; ' cookies are used to remember the values between HTTP sessions.
<BR>&nbsp;&nbsp;&nbsp; If Not (Request.QueryString("Text")="") Then
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Response.Cookies ("TextColor") = Request.QueryString("Text")
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Response.Cookies ("BackgroundColor") = Request.QueryString("Background")
<BR>&nbsp;&nbsp;&nbsp; End If
<BR>&nbsp; %&gt;
<BR>
<BR>&nbsp; &lt;html&gt;
<BR>&nbsp; &lt;head&gt;
<BR>&nbsp; &lt;/head&gt;
<BR>
<BR>&nbsp; &lt;%
<BR>&nbsp;&nbsp;&nbsp; 'Set colors according to existing previous user input.
<BR>&nbsp;&nbsp;&nbsp; If (Request.Cookies ("TextColor")="") Then %&gt;
<BR>&nbsp;&nbsp;&nbsp;&nbsp; &lt;body&gt;
<BR>&nbsp;&nbsp;&nbsp; &lt;% Else %&gt;
<BR>&nbsp;&nbsp;&nbsp;&nbsp; &lt;body bgcolor=&lt;%=Request.Cookies("BackgroundColor")%&gt; text=&lt;%=Request.Cookies("TextColor")%&gt;&gt;
<BR>&nbsp;&nbsp;&nbsp; &lt;% End If
<BR>&nbsp; %&gt;
<BR>
<BR>&nbsp; &lt;font face="MS Gothic"&gt;
<BR>
<BR>&nbsp; &lt;H2 align=center&gt;Select the colors for your Web page&lt;/H2&gt;
<BR>&nbsp; &lt;P&gt;
<BR>&nbsp; In Memory Cookies will be used to store these values.
<BR>&nbsp; &lt;/P&gt;
<BR>&nbsp; &lt;FORM ACTION="SelectColors.asp" METHOD="GET" NAME="DataForm"&gt;
<BR>&nbsp; &lt;table border="1" width="450" cellpadding=0&gt;
<BR>&nbsp; &lt;tr&gt;&lt;td&gt;
<BR>&nbsp;&nbsp;&nbsp; &lt;table&gt;
<BR>&nbsp;&nbsp;&nbsp; &lt;tr&gt;&lt;td BGCOLOR=99FF99&gt;
<BR>&nbsp;&nbsp;&nbsp; &lt;B&gt;&lt;font color=000000&gt;Please select the background color&lt;/font&gt;&lt;/B&gt;
<BR>&nbsp;&nbsp;&nbsp; &lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td BGCOLOR=FFFFFF&gt;
<BR>&nbsp;&nbsp;&nbsp; &lt;input type="RADIO" NAME="Background" VALUE="FFFFFF" CHECKED&gt;&lt;font COLOR=000000&gt; FFFFFF &lt;/font&gt;
<BR>&nbsp;&nbsp;&nbsp; &lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td BGCOLOR=D98719&gt;
<BR>&nbsp;&nbsp;&nbsp; &lt;input type="RADIO" NAME="Background" VALUE="D98719"&gt; D98719
<BR>&nbsp;&nbsp;&nbsp; &lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td BGCOLOR=D9D919&gt;
<BR>&nbsp;&nbsp;&nbsp; &lt;input type="RADIO" NAME="Background" VALUE="D9D919"&gt; D9D919
<BR>&nbsp;&nbsp;&nbsp; &lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td BGCOLOR=00FFFF&gt;
<BR>&nbsp;&nbsp;&nbsp; &lt;input type="RADIO" NAME="Background" VALUE="00FFFF"&gt; 00FFFF
<BR>&nbsp;&nbsp;&nbsp; &lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td BGCOLOR=FF00FF&gt;
<BR>&nbsp;&nbsp;&nbsp; &lt;input type="RADIO" NAME="Background" VALUE="FF00FF"&gt; FF00FF
<BR>&nbsp;&nbsp;&nbsp; &lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td BGCOLOR=000000&gt;
<BR>&nbsp;&nbsp;&nbsp; &lt;input type="RADIO" NAME="Background" VALUE="000000"&gt; &lt;font COLOR=FFFFFF&gt;000000&lt;/font&gt;
<BR>&nbsp;&nbsp;&nbsp; &lt;/td&gt;&lt;/tr&gt;
<BR>&nbsp; &lt;/table&gt;
<BR>&nbsp; &lt;/td&gt;&lt;td&gt;
<BR>&nbsp;&nbsp;&nbsp; &lt;table&gt;
<BR>&nbsp;&nbsp;&nbsp; &lt;tr&gt;&lt;td BGCOLOR=99FF99&gt;
<BR>&nbsp;&nbsp;&nbsp; &lt;B&gt;&lt;font color=000000&gt;Please select the text color&lt;/font&gt;&lt;/B&gt;
<BR>&nbsp;&nbsp;&nbsp; &lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td BGCOLOR=FFFFFF&gt;
<BR>&nbsp;&nbsp;&nbsp; &lt;input type="RADIO" NAME="Text" VALUE="FFFFFF" CHECKED&gt;&lt;font COLOR=000000&gt; FFFFFF &lt;/font&gt;
<BR>&nbsp;&nbsp;&nbsp; &lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td BGCOLOR=D98719&gt;
<BR>&nbsp;&nbsp;&nbsp; &lt;input type="RADIO" NAME="Text" VALUE="D98719"&gt; D98719
<BR>&nbsp;&nbsp;&nbsp; &lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td BGCOLOR=D9D919&gt;
<BR>&nbsp;&nbsp;&nbsp; &lt;input type="RADIO" NAME="Text" VALUE="D9D919"&gt; D9D919
<BR>&nbsp;&nbsp;&nbsp; &lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td BGCOLOR=00FFFF&gt;
<BR>&nbsp;&nbsp;&nbsp; &lt;input type="RADIO" NAME="Text" VALUE="00FFFF"&gt; 00FFFF
<BR>&nbsp;&nbsp;&nbsp; &lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td BGCOLOR=FF00FF&gt;
<BR>&nbsp;&nbsp;&nbsp; &lt;input type="RADIO" NAME="Text" VALUE="FF00FF"&gt; FF00FF
<BR>&nbsp;&nbsp;&nbsp; &lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td BGCOLOR=000000&gt;
<BR>&nbsp;&nbsp;&nbsp; &lt;input type="RADIO" NAME="Text" VALUE="000000" CHECKED&gt;&lt;font COLOR=FFFFFF&gt; 000000 &lt;/font&gt;
<BR>&nbsp;&nbsp;&nbsp; &lt;/td&gt;&lt;/tr&gt;
<BR>&nbsp;&nbsp;&nbsp;&nbsp; &lt;/table&gt;
<BR>&nbsp; &lt;/td&gt;&lt;/tr&gt;
<BR>&nbsp; &lt;/table&gt;
<BR>&nbsp; &lt;P&gt;
<BR>&nbsp; &lt;input type=Submit VALUE="Submit selected colors"&gt;
<BR>&nbsp; &lt;/FORM&gt;
<BR>
<BR>&nbsp; &lt;/font&gt;
<BR>&nbsp; &lt;/body&gt;
<BR>&nbsp; &lt;/html&gt;
</p>
</code>
<br><h4>DeleteColorCookie.asp</h4>
<p>
Open a new file in your text editor, paste in the following script, and save
the file as <B>C:\Inetpub\Wwwroot\Tutorial\DeleteColorCookie.asp</b>.
</p>
<code>
<p>
&nbsp; &lt;%@ Language="VBScript" %&gt;
<BR>
<BR>&nbsp; &lt;html&gt;
<BR>&nbsp; &lt;head&gt;
<BR>&nbsp; &lt;/head&gt;
<BR>&nbsp; &lt;body&gt;
<BR>&nbsp; &lt;font face="MS Gothic"&gt;
<BR>
<BR>&nbsp; &lt;%
<BR>&nbsp;&nbsp; If Not ("" = Request.Cookies("TextColor")) Then
<BR>&nbsp;&nbsp;&nbsp;&nbsp; Response.Cookies("TextColor").Expires = "January 1, 1992"
<BR>&nbsp;&nbsp;&nbsp;&nbsp; Response.Cookies("BackgroundColor").Expires = "January 1, 1992" %&gt;
<BR>
<BR>&nbsp;&nbsp;&nbsp;&nbsp; &lt;h2 align=center&gt;In-Memory Color Cookie Deleted&lt;/h2&gt;
<BR>&nbsp;&nbsp;&nbsp;&nbsp; &lt;P&gt;
<BR>&nbsp;&nbsp;&nbsp;&nbsp; The cookie used to keep track of your display colors has been deleted.&lt;BR&gt;
<BR>&nbsp;&nbsp;&nbsp;&nbsp; Please click on &lt;B&gt;Set Page Colors&lt;/B&gt; to set your colors again.
<BR>&nbsp;&nbsp;&nbsp;&nbsp; &lt;/P&gt;
<BR>
<BR>&nbsp;&nbsp; &lt;% Else %&gt;
<BR>
<BR>&nbsp;&nbsp;&nbsp;&nbsp; &lt;h2 align=center&gt;No In-Memory Color Cookie Deleted&lt;/h2&gt;
<BR>&nbsp;&nbsp;&nbsp;&nbsp; &lt;P&gt;
<BR>&nbsp;&nbsp;&nbsp;&nbsp; There was no cookie set with your color choices.&lt;BR&gt;
<BR>&nbsp;&nbsp;&nbsp;&nbsp; Please click on &lt;B&gt;Set Page Colors&lt;/B&gt; to set display colors.
<BR>&nbsp;&nbsp;&nbsp;&nbsp; &lt;/P&gt;
<BR>
<BR>&nbsp;&nbsp; &lt;% End If
<BR>&nbsp; %&gt;
<BR>
<BR>&nbsp; &lt;/font&gt;
<BR>&nbsp; &lt;/body&gt;
<BR>&nbsp; &lt;/html&gt;
</p>
</code>
<BR><h4>Cookie.asp</h4>
<p>
Open a new file in your text editor, paste in the following script, and save
the file as <B>C:\Inetpub\Wwwroot\Tutorial\Cookie.asp</b>.
</p>
<code>
<p>
&nbsp; &lt;%@ Language="VBScript" %&gt;
<BR>
<BR>&nbsp; &lt;%
<BR>&nbsp;&nbsp; LastAccessTime = Request.Cookies("LastTime")
<BR>&nbsp;&nbsp; LastAccessDate = Request.Cookies("LastDate")
<BR>
<BR>&nbsp;&nbsp; 'If the NumVisits cookie is empty, set to 0, else increment it.
<BR>&nbsp;&nbsp; If (Request.Cookies("NumVisits")="") Then
<BR>&nbsp;&nbsp;&nbsp;&nbsp; Response.Cookies("NumVisits") = 0
<BR>&nbsp;&nbsp; Else
<BR>&nbsp;&nbsp;&nbsp;&nbsp; Response.Cookies("NumVisits") = Request.Cookies("NumVisits") + 1
<BR>&nbsp;&nbsp; End If
<BR>
<BR>&nbsp;&nbsp; Response.Cookies("LastDate") = Date
<BR>&nbsp;&nbsp; Response.Cookies("LastTime") = Time
<BR>
<BR>&nbsp;&nbsp; 'Setting an expired date past the present date creates a persistent cookie.
<BR>&nbsp;&nbsp; Response.Cookies("LastDate").Expires = "January 15, 2001"
<BR>&nbsp;&nbsp; Response.Cookies("LastTime").Expires = "January 15, 2001"
<BR>&nbsp;&nbsp; Response.Cookies("NumVisits").Expires = "January 15, 2001"
<BR>&nbsp; %&gt;
<BR>
<BR>&nbsp; &lt;html&gt;
<BR>&nbsp; &lt;head&gt;
<BR>&nbsp; &lt;/head&gt;
<BR>&nbsp; &lt;% If (Request.Cookies ("TextColor")="") Then %&gt;
<BR>&nbsp;&nbsp;&nbsp;&nbsp; &lt;body&gt;
<BR>&nbsp;&nbsp;&nbsp;&nbsp; &lt;font face="MS Gothic"&gt;
<BR>&nbsp; &lt;% Else %&gt;
<BR>&nbsp;&nbsp;&nbsp;&nbsp; &lt;body bgcolor=&lt;%=Request.Cookies("BackgroundColor")%&gt; text=&lt;%=Request.Cookies("TextColor")%&gt;&gt;
<BR>&nbsp;&nbsp;&nbsp;&nbsp; &lt;font face="MS Gothic" color=&lt;%=Request.Cookies("TextColor")%&gt;&gt;
<BR>&nbsp; &lt;% End If %&gt;
<BR>
<BR>&nbsp; &lt;H2 align=center&gt;Persistent Client-Side Cookies!&lt;/H2&gt;
<BR>
<BR>&nbsp; &lt;P&gt;
<BR>&nbsp; Three persistent client-side cookies are created.
<BR>&nbsp; &lt;UL&gt;
<BR>&nbsp; &lt;LI&gt;A cookie to count the number of times you visited the Web page.&lt;/LI&gt;
<BR>&nbsp; &lt;LI&gt;A cookie to determine the date of your visit.&lt;/LI&gt;
<BR>&nbsp; &lt;LI&gt;A cookie to determine the time of your visit.&lt;/LI&gt;
<BR>&nbsp; &lt;/UL&gt;
<BR>&nbsp; &lt;/P&gt;
<BR>
<BR>&nbsp;&lt;table border="1" width="300" cellpadding=4 align=center&gt;
<BR>&nbsp;&lt;tr&gt;&lt;td&gt;
<BR>&nbsp;&lt;% If (Request.Cookies ("NumVisits")=0) Then %&gt;
<BR>&nbsp;&nbsp;&nbsp; Welcome! This is your first visit to this Web page!
<BR>&nbsp;&lt;% Else %&gt;
<BR>&nbsp;&nbsp;&nbsp; Thank you for visiting again! You have been to this Web page a total of &lt;B&gt;&lt;%=Request.Cookies("NumVisits")%&gt;&lt;/B&gt; time(s).
<BR>&nbsp;&lt;% End If %&gt;
<BR>&nbsp;&lt;/td&gt;&lt;/tr&gt;
<BR>&nbsp;&lt;/table&gt;
<BR>
<BR>&nbsp;&lt;P&gt;
<BR>&nbsp;&lt;B&gt;The Current time is &lt;%=Time%&gt; on &lt;%=Date%&gt;&lt;BR&gt;
<BR>&nbsp;&lt;% If (Request.Cookies ("NumVisits")&gt;0) Then %&gt;
<BR>&nbsp;&nbsp;&nbsp;&nbsp; You last visited this Web page at &lt;%=LastAccessTime%&gt; on &lt;%=LastAccessDate%&gt;
<BR>&nbsp;&lt;% End If %&gt;
<BR>&nbsp;&lt;/strong&gt;
<BR>&nbsp;&lt;/P&gt;
<BR>
<BR>&nbsp;&lt;/font&gt;
<BR>&nbsp;&lt;/body&gt;
<BR>&nbsp;&lt;/html&gt;
</p>
</code>
<br><h4>DeleteCookies.asp</h4>
<p>
Open a new file in your text editor, paste in the following script, and save
the file as DeleteCookies.asp.
</p>
<code>
<p>
<BR>&nbsp; &lt;%@ Language="VBScript" %&gt;
<BR>
<BR>&nbsp; &lt;html&gt;
<BR>&nbsp; &lt;head&gt;
<BR>&nbsp; &lt;/head&gt;
<BR>
<BR>&nbsp; &lt;% If (Request.Cookies ("TextColor")="") Then %&gt;
<BR>&nbsp;&nbsp;&nbsp;&nbsp; &lt;body&gt;
<BR>&nbsp;&nbsp;&nbsp;&nbsp; &lt;font face="MS Gothic"&gt;
<BR>&nbsp; &lt;% Else %&gt;
<BR>&nbsp;&nbsp;&nbsp;&nbsp; &lt;body bgcolor=&lt;%=Request.Cookies("BackgroundColor")%&gt; text=&lt;%=Request.Cookies("TextColor")%&gt;&gt;
<BR>&nbsp;&nbsp;&nbsp;&nbsp; &lt;font face="MS Gothic" color=&lt;%=Request.Cookies("TextColor")%&gt;&gt;
<BR>&nbsp; &lt;% End If %&gt;
<BR>
<BR>&nbsp; &lt;%
<BR>&nbsp;&nbsp; If Not ("" = Request.Cookies("NumVisits")) Then
<BR>&nbsp;&nbsp;&nbsp;&nbsp; Response.Cookies("NumVisits").Expires = "January 1, 1993"
<BR>&nbsp;&nbsp;&nbsp;&nbsp; Response.Cookies("LastDate").Expires = "January 1, 1993"
<BR>&nbsp;&nbsp;&nbsp;&nbsp; Response.Cookies("LastTime").Expires = "January 1, 1993" %&gt;
<BR>
<BR>&nbsp;&nbsp;&nbsp;&nbsp; &lt;H2 align=center&gt;Persistent Cookies Are Deleted&lt;/H2&gt;
<BR>&nbsp;&nbsp;&nbsp;&nbsp; &lt;P&gt;
<BR>&nbsp;&nbsp;&nbsp;&nbsp; The cookies used to keep track of your visits and date and time of last visit have been deleted.&lt;BR&gt;
<BR>&nbsp;&nbsp;&nbsp;&nbsp; Please click on &lt;B&gt;Set Cookies for Date, Time and Total Visits&lt;/B&gt; to set your cookies again.
<BR>&nbsp;&nbsp;&nbsp;&nbsp; &lt;/P&gt;
<BR>
<BR>&nbsp;&nbsp; &lt;% Else %&gt;
<BR>
<BR>&nbsp;&nbsp;&nbsp;&nbsp; &lt;H2 align=center&gt;No Persistent Cookies Are Deleted&lt;/H2&gt;
<BR>&nbsp;&nbsp;&nbsp;&nbsp; &lt;P&gt;
<BR>&nbsp;&nbsp;&nbsp;&nbsp; There were no cookies set to keep track of your visits, and date and time of last visit.&lt;BR&gt;
<BR>&nbsp;&nbsp;&nbsp;&nbsp; Please click on &lt;B&gt;Set Cookies for Date, Time and Total Visits&lt;/B&gt; to set your colors again.
<BR>&nbsp;&nbsp;&nbsp;&nbsp; &lt;/P&gt;
<BR>
<BR>&nbsp;&nbsp; &lt;% End If %&gt;
<BR>
<BR>&nbsp; &lt;/font&gt;
<BR>&nbsp; &lt;/body&gt;
<BR>&nbsp; &lt;/html&gt;
</p>
</code>
<BR><h3>Cookies Using the Session Object</h3>
<p>
With the <b>Session</b> object, you can create only an in-memory cookie. For the
<b>Session</b> object to work correctly, you need to determine when a user's visit
to the site begins and ends. IIS does this by using a cookie that stores an
ASP Session ID, which is used to maintain a set of information about
a user. If an ASP Session ID is not present, the server considers the current
request to be the start of a visit. The visit ends when there have been no
user requests for ASP files for the default time period of 20 minutes.
</p>
<p>
In this lesson, you will create the following:
</p>
<ul>
<li>
<B>Global.asa</B>&nbsp;&nbsp;&nbsp;Global.asa is a file that allows you to perform generic
actions at the beginning of the application and at the beginning of each user's
session. An application starts the first time the first user ever requests a
page and ends when the application is unloaded or when the server is taken
offline. A unique session starts once for each user and
ends 20 minutes after that user has requested their last page. Generic
actions you can perform in Global.asa include setting application or session
variables, authenticating a user, logging the date and time that a user
connected, instantiating COM objects that remain active for an entire application
or session, and so forth.
</li><li>
<B>VisitCount.asp</B>&nbsp;&nbsp;&nbsp;This ASP script uses the <b>Session</b> object to create an
in-memory cookie.
</li></ul>
<p>
When an application or session begins or ends, it is considered an event.
Using the Global.asa file, you can use the predefined event procedures that
run in response to the event.
</p>
<BR><h4>Global.asa </h4>
<p>
Open a new file in your text editor, paste in the following script, and save
the file in your root directory as <B>C:\Inetpub\Wwwroot\Global.asa</B>.
</P><P>
<B>Important:</B> Global.asa files must be saved in the root directory of
the application for ASP to find it. If you had a virtual path called Test
mapped to C:\Inetpub\Wwwroot\Test, your URL would be http://LocalHost/Test, and
the Global.asa file would have to go in C:\Inetpub\Wwwroot\Test. We did not
create a virtual path mapped to C:\Inetpub\Wwwroot\Tutorial, so our root
directory is still C:\Inetpub\Wwwroot.
</p>
<code>
<p>
&nbsp; &lt;SCRIPT LANGUAGE=VBScript RUNAT=Server&gt;
<BR>
<BR>&nbsp; 'Using application-level variables to track the number of users
<BR>&nbsp; ' that are currently looking at the site and the number that have
<BR>&nbsp; ' accessed the site.
<BR>&nbsp; Sub Application_OnStart
<BR>
<BR>&nbsp;&nbsp;&nbsp; 'Get the physical path to this vdir, and append a filename.
<BR>&nbsp;&nbsp;&nbsp; Application("PhysPath") = Server.MapPath(".") & "\hits.txt"
<BR>
<BR>&nbsp;&nbsp;&nbsp; 'Set some Visual Basic constants, and instantiate the FileSystemObject object.
<BR>&nbsp;&nbsp;&nbsp; Const cForReading = 1
<BR>&nbsp;&nbsp;&nbsp; Const cTristateUseDefault = -2
<BR>&nbsp;&nbsp;&nbsp; Set fsoObject = Server.CreateObject("Scripting.FileSystemObject")
<BR>
<BR>&nbsp;&nbsp;&nbsp; 'Get the last saved value of page hits and the date that it happened.
<BR>&nbsp;&nbsp;&nbsp; If fsoObject.FileExists(Application("PhysPath")) Then
<BR>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 'If the file hits.txt exists, set the Application variables.
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Set tsObject = fsoObject.OpenTextFile(Application("PhysPath"), cForReading, cTristateUseDefault)
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Application("HitCounter") = tsObject.ReadLine
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Application("AppStartDate") = tsObject.ReadLine
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; tsObject.Close
<BR>
<BR>&nbsp;&nbsp;&nbsp; Else 'No file has been saved, so reset the values.
<BR>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Application("HitCounter") = 0
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Application("AppStartDate") = Date
<BR>
<BR>&nbsp;&nbsp;&nbsp; End If
<BR>
<BR>&nbsp;&nbsp;&nbsp; Application("CurrentUsers") = 0
<BR>
<BR>&nbsp; End Sub
<BR>
<BR>
<BR>&nbsp; Sub Application_OnEnd
<BR>
<BR>&nbsp;&nbsp;&nbsp; Const cForWriting = 2
<BR>&nbsp;&nbsp;&nbsp; Const cTristateUseDefault = -2
<BR>
<BR>&nbsp;&nbsp;&nbsp; Set fsoObject = Server.CreateObject("Scripting.FileSystemObject")
<BR>&nbsp;&nbsp;&nbsp; If fsoObject.FileExists(Application("PhysPath")) Then
<BR>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 'If the file exists, open it for writing.
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; set tsObject = fsoObject.OpenTextFile(Application("PhysPath"), cForWriting, cTristateUseDefault)
<BR>
<BR>&nbsp;&nbsp;&nbsp; Else
<BR>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 'If the file doesn't exist, create a new one.
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; set tsObject = fsoObject.CreateTextFile(Application("PhysPath"))
<BR>
<BR>&nbsp;&nbsp;&nbsp; End If
<BR>
<BR>&nbsp;&nbsp;&nbsp; 'Write the total number of site hits and the last day recorded to the file.
<BR>&nbsp;&nbsp;&nbsp; tsObject.WriteLine(Application("HitCounter"))
<BR>&nbsp;&nbsp;&nbsp; tsObject.WriteLine(Application("AppStartDate"))
<BR>&nbsp;&nbsp;&nbsp; tsObject.Close
<BR>
<BR>&nbsp; End Sub
<BR>
<BR>
<BR>&nbsp; Sub Session_OnStart
<BR>
<BR>&nbsp;&nbsp;&nbsp; 'The Session time-out default is changed to 1 for the purposes of
<BR>&nbsp;&nbsp;&nbsp; ' this example.
<BR>&nbsp;&nbsp;&nbsp; Session.Timeout = 1
<BR>
<BR>&nbsp;&nbsp;&nbsp; 'When you change Application variables, you must lock them so that other
<BR>&nbsp;&nbsp;&nbsp; ' sessions cannot change them at the same time.
<BR>&nbsp;&nbsp;&nbsp; Application.Lock
<BR>
<BR>&nbsp;&nbsp;&nbsp; 'Increment the site hit counter.
<BR>&nbsp;&nbsp;&nbsp; Application("HitCounter") = Application("HitCounter") + 1
<BR>&nbsp;&nbsp;&nbsp; Application("CurrentUsers") = Application("CurrentUsers") + 1
<BR>
<BR>&nbsp;&nbsp;&nbsp; Application.UnLock
<BR>
<BR>&nbsp; End Sub
<BR>
<BR>
<BR>&nbsp; Sub Session_OnEnd
<BR>
<BR>&nbsp;&nbsp;&nbsp; Application.Lock
<BR>
<BR>&nbsp;&nbsp;&nbsp; 'Decrement the current user counter.
<BR>&nbsp;&nbsp;&nbsp; Application("CurrentUsers") = Application("CurrentUsers") - 1
<BR>
<BR>&nbsp;&nbsp;&nbsp; Application.UnLock
<BR>
<BR>&nbsp; End Sub
<BR>
<BR>&nbsp; &lt;/SCRIPT&gt;
</p>
</code>
<BR><h4>VisitCount.asp</h4>
<p>
You can use variables set in Global.asa to measure visits and sessions.
</p>
<p>
Open a new file in your text editor, paste in the following script, and save
the file as <B>C:\Inetpub\Wwwroot\Tutorial\VisitCount.asp</B>. View the file
in your browser by typing
http&#58;&#47;&#47;Localhost&#47;Tutorial&#47;VisitCount.asp. </p>
<p>Open a second instance of the browser to
http&#58;&#47;&#47;Localhost&#47;Tutorial&#47;VisitCount.asp,
and click <B>Refresh</B> on the first browser. Total Visitors and Active Visitors
should increase by one. Close down the second browser, wait over a minute, and
click <B>Refresh</B> on the first browser. Active Visitors should decrease by one.
</p>
<code>
<p>
&nbsp; &lt;% Response.Buffer = True%&gt;
<BR>
<BR>&nbsp; &lt;html&gt;
<BR>&nbsp; &lt;head&gt;
<BR>&nbsp; &lt;title&gt;Retrieving Variables Set in Global.asa&lt;/title&gt;
<BR>&nbsp; &lt;/head&gt;
<BR>&nbsp; &lt;body&gt;
<BR>&nbsp; &lt;font face="MS Gothic"&gt;
<BR>
<BR>&nbsp; &lt;H3 align=center&gt;Retrieving Variables Set in Global.asa&lt;/H3&gt;
<BR>&nbsp; &lt;P&gt;
<BR>&nbsp; Total Visitors = &lt;%=Application("HitCounter")%&gt; since &lt;%=Application("AppStartDate")%&gt;&lt;BR&gt;
<BR>&nbsp; Active Visitors = &lt;%=Application("CurrentUsers")%&gt;
<BR>&nbsp; &lt;/P&gt;
<BR>
<BR>&nbsp; &lt;/font&gt;
<BR>&nbsp; &lt;/body&gt;
<BR>&nbsp; &lt;/html&gt;
</p>
</code>
<BR><h2><a name="without">Maintaining Session State Without Cookies</a></h2>
<p>
Some browsers do not recognize cookies, and users can choose to disable cookies
in their browsers. The HTTP POST method provides an alternative to cookies
to maintain session state. The HTTP POST method provides the same
state information as would a cookie but has the advantage that it works even
when cookies are not available. This method is not common in practice, but it
is a good example to learn from. The HTTP POST method works similarly to an
in-memory cookie; user information can be maintained only during the visit, and
the session state information is gone when the user turns off the browser.
</p>
<BR><h3>DataEntry.asp</h3>
<p>
Open a new file in your text editor, paste in the following script, and save
the files as <B>C:\Inetpub\Wwwroot\Tutorial\DataEntry.asp</b>. View the file in your browser by typing
http&#58;&#47;&#47;Localhost&#47;Tutorial&#47;DataEntry.asp.
</p>
<code>
<p>
&nbsp; &lt;%@ Language=VBScript %&gt;
<BR>
<BR>&nbsp; &lt;html&gt;
<BR>&nbsp; &lt;head&gt;
<BR>&nbsp; &lt;title&gt;Data Entry Without Cookies&lt;/title&gt;
<BR>&nbsp; &lt;/head&gt;
<BR>&nbsp; &lt;body&gt;
<BR>&nbsp; &lt;font face="MS Gothic"&gt;
<BR>
<BR>&nbsp; &lt;!-- In this example, subroutines are listed first.
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; There's a subroutine for each page of the order process.
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; The main calling code is at the bottom. --&gt;
<BR>
<BR>&nbsp; &lt;% Sub DisplayInitialPage %&gt;
<BR>
<BR>&nbsp;&nbsp;&nbsp; &lt;table border=1 cellpadding=3 cellspacing=0 width=500 bordercolor=#808080 align=center&gt;
<BR>&nbsp;&nbsp;&nbsp; &lt;tr&gt;&lt;td bgColor=#004080 align=center&gt;
<BR>&nbsp;&nbsp;&nbsp; &lt;font color=#ffffff&gt;&lt;H2&gt;Order Form&lt;/H2&gt;&lt;/font&gt;
<BR>&nbsp;&nbsp;&nbsp; &lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td bgColor=#e1e1e1 align=left&gt;
<BR>&nbsp;&nbsp;&nbsp; &lt;P&gt;&lt;B&gt;Step 1 of 4&lt;/B&gt;&lt;/P&gt;
<BR>&nbsp;&nbsp;&nbsp; &lt;P align=center&gt;
<BR>&nbsp;&nbsp;&nbsp; This form uses the HTTP POST method to pass along hidden values that contain
<BR>&nbsp;&nbsp;&nbsp; your order information. This form does not use cookies.
<BR>&nbsp;&nbsp;&nbsp; &lt;/P&gt;
<BR>
<BR>&nbsp;&nbsp;&nbsp; &lt;FORM METHOD=POST ACTION="DataEntry.asp" NAME=DataEntryForm&gt;
<BR>&nbsp;&nbsp;&nbsp; &lt;P&gt;Enter your name
<BR>&nbsp;&nbsp;&nbsp; &lt;INPUT TYPE="TEXT" NAME=FullName&gt;
<BR>&nbsp;&nbsp;&nbsp; &lt;BR&gt;Enter your imaginary credit card number
<BR>&nbsp;&nbsp;&nbsp; &lt;INPUT TYPE="TEXT" NAME=CreditCard&gt;
<BR>&nbsp;&nbsp;&nbsp; &lt;/P&gt;
<BR>&nbsp;&nbsp;&nbsp; &lt;!-- Keeps track of the information by using the hidden HTML form variable Next Page. --&gt;
<BR>&nbsp;&nbsp;&nbsp; &lt;INPUT TYPE="HIDDEN" NAME=NextPage VALUE=2&gt;
<BR>&nbsp;&nbsp;&nbsp; &lt;INPUT TYPE="SUBMIT" VALUE="Next -&gt;" NAME=NextButton&gt;
<BR>&nbsp;&nbsp;&nbsp; &lt;/FORM&gt;
<BR>
<BR>&nbsp;&nbsp;&nbsp; &lt;/td&gt;&lt;/tr&gt;
<BR>&nbsp;&nbsp;&nbsp; &lt;/table&gt;
<BR>
<BR>&nbsp; &lt;% End Sub %&gt;
<BR>
<BR>
<BR>&nbsp; &lt;% Sub DisplayDogBreed %&gt;
<BR>
<BR>&nbsp;&nbsp;&nbsp; &lt;table border=1 cellpadding=3 cellspacing=0 width=500 align=center&gt;
<BR>&nbsp;&nbsp;&nbsp; &lt;tr&gt;&lt;td bgColor=#004080 align=center&gt;
<BR>&nbsp;&nbsp;&nbsp; &lt;font color=#ffffff&gt;&lt;H2&gt;Order Form&lt;/H2&gt;&lt;/font&gt;
<BR>&nbsp;&nbsp;&nbsp; &lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td bgColor=#e1e1e1&gt;
<BR>&nbsp;&nbsp;&nbsp; &lt;P&gt;&lt;B&gt;Step 2 of 4&lt;/B&gt;&lt;/P&gt;
<BR>&nbsp;&nbsp;&nbsp; &lt;P align=center&gt;
<BR>&nbsp;&nbsp;&nbsp; Please select the type of dog you want.
<BR>&nbsp;&nbsp;&nbsp; &lt;/P&gt;
<BR>
<BR>&nbsp;&nbsp;&nbsp; &lt;FORM METHOD=POST ACTION="DataEntry.asp" NAME=DataEntryForm&gt;
<BR>&nbsp;&nbsp;&nbsp; &lt;P&gt;
<BR>&nbsp;&nbsp;&nbsp; &lt;INPUT TYPE=RADIO NAME=DogSelected VALUE="Cocker Spaniel" CHECKED&gt;Cocker Spaniel&lt;BR&gt;
<BR>&nbsp;&nbsp;&nbsp; &lt;INPUT TYPE=RADIO NAME=DogSelected VALUE="Doberman"&gt;Doberman&lt;BR&gt;
<BR>&nbsp;&nbsp;&nbsp; &lt;INPUT TYPE=RADIO NAME=DogSelected VALUE="Timber Wolf"&gt;Timber Wolf&lt;BR&gt;
<BR>&nbsp;&nbsp;&nbsp; &lt;INPUT TYPE=RADIO NAME=DogSelected VALUE="Mastiff"&gt;Mastiff&lt;BR&gt;
<BR>&nbsp;&nbsp;&nbsp; &lt;/P&gt;
<BR>&nbsp;&nbsp;&nbsp; &lt;!--Keeps track of the information by using the hidden HTML form variable Next Page. --&gt;
<BR>&nbsp;&nbsp;&nbsp; &lt;INPUT TYPE="HIDDEN" NAME=NextPage VALUE=3&gt;
<BR>&nbsp;&nbsp;&nbsp; &lt;INPUT TYPE="SUBMIT" VALUE="Next -&gt;" NAME=NextButton&gt;
<BR>&nbsp;&nbsp;&nbsp; &lt;/FORM&gt;
<BR>&nbsp;&nbsp;&nbsp; &lt;/td&gt;&lt;/tr&gt;
<BR>&nbsp;&nbsp;&nbsp; &lt;/table&gt;
<BR>
<BR>&nbsp; &lt;% End Sub %&gt;
<BR>
<BR>
<BR>&nbsp; &lt;% Sub DisplayCity %&gt;
<BR>
<BR>&nbsp;&nbsp;&nbsp; &lt;table border=1 cellpadding=3 cellspacing=0 width=500 align=center&gt;
<BR>&nbsp;&nbsp;&nbsp; &lt;tr&gt;&lt;td bgColor=#004080 align=center&gt;
<BR>&nbsp;&nbsp;&nbsp; &lt;font color=#ffffff&gt;&lt;H2&gt;Order Form&lt;/H2&gt;&lt;/font&gt;
<BR>&nbsp;&nbsp;&nbsp; &lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td bgColor=#e1e1e1&gt;
<BR>&nbsp;&nbsp;&nbsp; &lt;P&gt;&lt;B&gt;Step 3 of 4&lt;/B&gt;&lt;/P&gt;
<BR>&nbsp;&nbsp;&nbsp; &lt;P align=center&gt;
<BR>&nbsp;&nbsp;&nbsp; We deliver from the following cities. Please choose the one closest to you.
<BR>&nbsp;&nbsp;&nbsp; &lt;/P&gt;
<BR>
<BR>&nbsp;&nbsp;&nbsp; &lt;FORM METHOD=POST ACTION="DataEntry.asp" NAME=DataEntryForm&gt;
<BR>&nbsp;&nbsp;&nbsp; &lt;P&gt;
<BR>&nbsp;&nbsp;&nbsp; &lt;INPUT TYPE=RADIO NAME=CitySelected VALUE="Seattle" CHECKED&gt;Seattle&lt;BR&gt;
<BR>&nbsp;&nbsp;&nbsp; &lt;INPUT TYPE=RADIO NAME=CitySelected VALUE="Los Angeles"&gt;Los Angeles&lt;BR&gt;
<BR>&nbsp;&nbsp;&nbsp; &lt;INPUT TYPE=RADIO NAME=CitySelected VALUE="Boston"&gt;Boston&lt;BR&gt;
<BR>&nbsp;&nbsp;&nbsp; &lt;INPUT TYPE=RADIO NAME=CitySelected VALUE="New York"&gt;New York&lt;BR&gt;
<BR>&nbsp;&nbsp;&nbsp; &lt;/P&gt;
<BR>&nbsp;&nbsp;&nbsp; &lt;!--Keeps track of the information by using the hidden HTML form variable Next Page. --&gt;
<BR>&nbsp;&nbsp;&nbsp; &lt;INPUT TYPE="HIDDEN" NAME=NextPage VALUE=4&gt;
<BR>&nbsp;&nbsp;&nbsp; &lt;INPUT TYPE="SUBMIT" VALUE="Next -&gt;" NAME=NextButton&gt;
<BR>&nbsp;&nbsp;&nbsp; &lt;/FORM&gt;
<BR>&nbsp;&nbsp;&nbsp; &lt;/td&gt;&lt;/tr&gt;
<BR>&nbsp;&nbsp;&nbsp; &lt;/table&gt;
<BR>
<BR>&nbsp; &lt;% End Sub %&gt;
<BR>
<BR>
<BR>&nbsp; &lt;% Sub DisplaySummary %&gt;
<BR>
<BR>&nbsp;&nbsp;&nbsp; &lt;table border=1 cellpadding=3 cellspacing=0 width=500 align=center&gt;
<BR>&nbsp;&nbsp;&nbsp; &lt;tr&gt;&lt;td bgColor=#004080 align=center&gt;
<BR>&nbsp;&nbsp;&nbsp; &lt;font color=#ffffff&gt;&lt;H2&gt;Order Form Completed&lt;/H2&gt;&lt;/font&gt;
<BR>&nbsp;&nbsp;&nbsp; &lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td bgColor=#e1e1e1&gt;
<BR>&nbsp;&nbsp;&nbsp; &lt;P&gt;&lt;B&gt;Step 4 of 4&lt;/B&gt;&lt;/P&gt;
<BR>&nbsp;&nbsp;&nbsp; &lt;P align=center&gt;
<BR>&nbsp;&nbsp;&nbsp; The following information was entered.&lt;BR&gt;
<BR>&nbsp;&nbsp;&nbsp; A transaction will now be executed to complete your order if your name and
<BR>&nbsp;&nbsp;&nbsp; credit card are valid.
<BR>&nbsp;&nbsp;&nbsp; &lt;/P&gt;
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;table cellpadding=4&gt;
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;tr bgcolor=#ffffcc&gt;&lt;td&gt;
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Name
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;/td&gt;&lt;td&gt;
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;%=Session.Value("FullName")%&gt;
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;/td&gt;&lt;/tr&gt;&lt;tr bgcolor=Beige&gt;&lt;td&gt;
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Credit Card
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;/td&gt;&lt;td&gt;
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;%=Session.Value("CreditCard")%&gt;
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;/td&gt;&lt;/tr&gt;&lt;tr bgcolor=Beige&gt;&lt;td&gt;
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Dog Ordered
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;/td&gt;&lt;td&gt;
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;%=Session.Value("DogSelected")%&gt;
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;/td&gt;&lt;/tr&gt;&lt;tr bgcolor=Beige&gt;&lt;td&gt;
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; City Ordered From
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;/td&gt;&lt;td&gt;
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;%=Session.Value("CitySelected")%&gt;
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;/td&gt;&lt;/tr&gt;
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;/table&gt;
<BR>&nbsp;&nbsp;&nbsp; &lt;/td&gt;
<BR>&nbsp;&nbsp;&nbsp; &lt;/tr&gt;
<BR>&nbsp;&nbsp;&nbsp; &lt;/table&gt;
<BR>
<BR>&nbsp; &lt;% End Sub %&gt;
<BR>
<BR>
<BR>&nbsp; &lt;% Sub StoreUserDataInSessionObject %&gt;
<BR>&nbsp; &lt;%
<BR>&nbsp;&nbsp;&nbsp; Dim FormKey
<BR>&nbsp;&nbsp;&nbsp; For Each FormKey in Request.Form
<BR>&nbsp;&nbsp;&nbsp; Session(FormKey) = Request.Form.Item(FormKey)
<BR>&nbsp;&nbsp;&nbsp; Next
<BR>&nbsp; %&gt;
<BR>&nbsp; &lt;% End Sub %&gt;
<BR>
<BR>
<BR>&nbsp; &lt;%
<BR>&nbsp;&nbsp;&nbsp; 'This is the main code that calls all the subroutines depending on the
<BR>&nbsp;&nbsp;&nbsp; ' hidden form elements.
<BR>
<BR>&nbsp;&nbsp;&nbsp; Dim CurrentPage
<BR>
<BR>&nbsp;&nbsp;&nbsp; If Request.Form.Item("NextPage") = "" Then
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; CurrentPage = 1
<BR>&nbsp;&nbsp;&nbsp; Else
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; CurrentPage = Request.Form.Item("NextPage")
<BR>&nbsp;&nbsp;&nbsp; End If
<BR>
<BR>&nbsp;&nbsp;&nbsp; 'Save all user data so far.
<BR>&nbsp;&nbsp;&nbsp; Call StoreUserDataInSessionObject
<BR>
<BR>&nbsp;&nbsp;&nbsp; Select Case CurrentPage
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Case 1 : Call DisplayInitialPage
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Case 2 : Call DisplayDogBreed
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Case 3 : Call DisplayCity
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Case 4 : Call DisplaySummary
<BR>&nbsp;&nbsp;&nbsp; End Select %&gt;
<BR>
<BR>&nbsp; &lt;BR&gt;
<BR>&nbsp; &lt;HR&gt;
<BR>&nbsp; &lt;H3 align=center&gt;&lt;A HREF="DataEntry.asp"&gt;Reset Order&lt;/A&gt;&lt;/H3&gt;
<BR>
<BR>&nbsp; &lt;/font&gt;
<BR>&nbsp; &lt;/body&gt;
<BR>&nbsp; &lt;/html&gt;
</p>
</code>
<hr class="iis" size="1">
<p align=center><a href="/iishelp/common/colegal.htm"><em>&copy; 1997-2001 Microsoft Corporation. All rights reserved.</em></a></p>
</font>
</body>
</html>