

ASP ADVANCED
Two components included
with Active Server Pages can be used to create page counters.
Using page counters, you can track the number of times that a
particular page has been requested. You
can display this information on the page itself, or you can keep track of
this information for your own purposes.
The Counters
Component
The Counters component
can be used to count the number of times a page has been requested, but it
can also be used to count anything else as well.
For example, you can use it to count the number of visitors to your
Web site, the number of times an advertisement has been clicked, or even
the number of times someone has requested a Web page with the Netscape 2.0
browser.
You can create only one
instance of this component. However,
once you create an instance of the component, you can create as many
individual counters as you need. The
single Counter component can contain many individual counters with
different names.
Because you can create only
one Counters component, it's a good idea to create the Component within
the Global.asa file. This
will guarantee that only one instance of the Counters component is created
when your Web server starts.
Here's an example of how
you can create the component within the Global.asa file:
<OBJECT RUNAT="Server"
SCOPE="Application" ID="MyCount"
PROGID="MSWC.counters"></OBJECT>
The Microsoft extended HTML
<OBJECT> tag is used here to create an instance of the Counters
component named MyCount with application-wide scope.
Remember to use the <OBJECT> tag outside any scripts within
the Global.asa file. Once an
instance of the Counters component has been created in this way, its
methods can be accessed from any page within your specific application.
The Counters
component has four methods. The
following list details how each method is
used:
- Get (counter name).
This
method returns the current value of a counter.
If the counter doesn't exist, it is created and set to 0.
- Increment
(counter name). This
method adds 1 to the current value of a counter.
If the counter doesn't exist, it is created and its value
set to 1.
- Remove (counter
name). This
method destroys a counter.
-
set (counter name, integer). This
method accepts two arguments. The
first argument is the name of a counter, and the second argument is an
integer value. The method
adds the integer to the counter.
If the counter doesn't exist, the counter is created with the
specified value.
Once an instance of
the Counters component has been created in the Global.asa file, you can
increment and decrement individual
counters within any Active Server Page.
A counter created in one page can be incremented, decremented, or
removed in another page. Following
is an example of how you can use the Counters component to keep track of
the number of times a particular page has been requested:
<HTML>
<HEAD><TITLE>Some Page</TITLE></HEAD>
<BODY>
This page has been requested
<%=Mycount.Increment("PageCnt") %>
times.
</BODY>
</HTML>
The first time this page is
requested, a counter named PageCnt is created and set to the value 1.
Subsequent requests increment the value of this counter by 1. The PageCnt
counter reflects the number of times the page has been requested.
What happens if your server
is unexpectedly shut down? The
counters you create with the Counters component are persistent.
They're saved in a file named counters.txt.
If the server shuts down, the counters still exist when it starts
again.
Admittedly, the counter is
a little boring. Most
counters you see on Web sites use images for their counters.
You can do this as well:
<%
SUB ShowImagoCnt(TheNum)
CntStr=CSTR(TheNum)
FOR i=1 TO LEN(CntStr)
CntPart=MID(CntStr,i,1)
%>
<IMG SRC="<%=CntPart%>.gif" ALT="<%=CntPart%>">
<%
NEXT
END SUB
%>
<HTML>
<HEAD><TITLE>Some Page</TITLE></HEAD>
<BODY>
This page has been requested-
<%
ShowImageCnt MyCount.Increment("PageCnt")
%>
times.
</BODY>
</HTML>
This new Active Server Page
also displays a page counter. However,
the page count is displayed by using images rather than text. The
procedure named ShowImageCnt first converts the page count to a string.
Next, a FOR...NEXT loop is used to walk through each numeral in the
string and display a corresponding image.
To use this example, you'll
need 10 images named 0.gif, 1.gif, 2.gif, and so on.
You can create these images yourself However, a number of Internet
sites have libraries of counter images that you can download freely (check
the graphics section at your favorite Internet directory).
The Page Counter
Component
There's a second component
that you can use to display a page counter on a Web page.
By using the Page Counter component, you can track the
number of times a particular Web page has been opened.
The Page Counter component
is much less flexible than the Counters component.
It can't be used to track anything other than a page's hit count.
This component has the following two methods:
- Hits (path).
This
method returns the number of times a page with the specified path has
been opened. If no path
is provided, the method returns this value for the current page.
- Reset (path).
This method resets the
count to 0 for the page with the specified path.
If no path is provided, the value for the
current page is reset.
Unlike
when using the Counters component, you don't need to create an instance of
the Page Counter component within the Global.asa file.
You can create an instance of the component in the same page in
which you use it, like this:
<HTML>
<HEAD><TITLE> Page Counter Example </TITLE></HEAD>
<BODY>
<%
Set MyHits=Server.CreateObject("MSWC.PageCounter")
%>
This page has been viewed
<%=MyHits.Hits%>
times.
</BODY>
</HTML>
This
Active Server Page simply displays the number of times the current page
has been opened. The Hits
method is called without any parameters, which results in the hit count
for the current page being returned.
Summary
This
chapter showed you how to incorporate advertisements into your Web pages.
You learned how to use the Ad Rotator component to display a series
of banner advertisements. You
also learned how to use the Content Rotator component to randomly display
different HTML content strings. Finally,
you learned how to use two components that can be used to display a page
counter on a Web page.
|