Counters for Active Server Pages using XML (part 2)
Page CounterUsing these functions to count how many times an ASP page is loaded is simple. Include the functions in your ASP page the include statement:
Then you can increment the counter:
The IncCounter function also returns the current count so to increment and display the current count:
<% IncCounter "Page1" %>
Or to simply display the current count:
<%= IncCounter("Page1") %>
Or you can make a generic snippet of code to include on each page you wish to count:
<%= GetCounter("Page1") %>
This will create a counter with the file name of the script.
<% Counter = Response.ServerVariables("SCRIPT_NAME") IncCounter Counter %>
Session CounterWhat may be more useful to know is how many sessions have been established? This can be done through the global.asa file. Each time a new session is called the Session_OnStart subroutine is called. Just include the IncCounter code above (I don't think that the global.asa file can use sub functions):
Then every time a new session is established the "sessions" count is incremented.
<SCRIPT LANGUAGE=VBScript RUNAT=Server> Sub Session_OnStart Dim CountName CountName = "sessions" Dim rootNode Dim Count Dim Found Set xmlDOC = Server.CreateObject("Microsoft.XMLDOM") xmlDOC.async = false xmlDOC.load Server.MapPath("counts.xml") Found = False Count = 1 Set rootNode = xmlDOC.selectSingleNode("counts") For Each Node in rootNode.selectNodes("count") If Node.selectSingleNode("name").Text = CountName Then Found = True Exit For End if Next If Not Found Then Set Node = rootNode.appendChild( _ rootNode.selectSingleNode("count").CloneNode(TRUE)) Node.selectSingleNode("name").Text = CountName End If Count = Count + 1 Node.selectSingleNode("count").Text = Count xmlDOC.save xmlSRC End Sub </SCRIPT>
Image CounterNow what if you wish to count how many time an image is viewed? If the you have a page that resides on another server this is an easy way to count page hits. I'm sure many of you have seen this done before. The following text is saved in the file "counter.gif.asp":
<% ' Clear out the existing HTTP header information Response.Expires = 0 Response.Buffer = TRUE %> <!--#include file="counter.inc.asp"--> <% CounterName = Request.QueryString("cName") IncCounter CounterName Response.Clear Response.Redirect "counter.gif" % >
This ASP file first clears the HTTP header information so that the client doesn't think it is getting a text file, counter in incremented, and the browser is redirected to the counter.gif image file.
To use the IMG counter just include a img link in the page you wish to count:
The "counter.gif.asp" file can be easily modified so that you can select the image file to be returned.
Now say you wished to count how many times a particular link is clicked. This can be done by linking to a page that uses the IncFunction to count (similar to the image counter) but then redirects the browser to a new URL, redirect.asp:
Then simply link with
<% ' Clear out the existing HTTP header information Response.Expires = 0 Response.Buffer = TRUE %> <!--#include file=" counter.inc.asp "--> <% URL = Request.QueryString("URL") CounterName = Request.QueryString("cName") IncCounter(CounterName) Response.Redirect URL %>
Download CounterIf you wish to count how many times a file is downloaded you can use the same "redirect.asp" file as above. However, you may wish to display a page while the file is downloading. You can include the following code in an asp file:
Followed by the HTML you wish to display while the file is being downloaded. This can be a notice that the file is being downloaded or it can be instructions for installation.