Counters for Active Server Pages using XML (part 2)
Page Counter
Using these functions to count how many times an ASP page is loaded is simple. Include the functions in your ASP page the include statement:<!--#include file="counter.inc.asp"-->
Then you can increment the counter:
<% IncCounter "Page1" %>
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:
<%= GetCounter("Page1") %>
Or you can make a generic snippet of code to include on each page you wish to count:
<% Counter = Response.ServerVariables("SCRIPT_NAME")
IncCounter Counter
%>
This will create a counter with the file name of the script.
Session Counter
What 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):<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>
Then every time a new session is established the "sessions" count is incremented.
Image Counter
Now 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:
<img src="counter.gif.asp?cName=CounterName">
The "counter.gif.asp" file can be easily modified so that you can select the image file to be returned.
Link Counter
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:
<%
' 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
%>
Then simply link with
<a href="redirect.asp?cName=CounterName&URL=page1.asp">
Download Counter
If 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:<!--#include file=" counter.inc.asp "-->
<%
URL = Request.QueryString("URL")
CounterName = Request.QueryString("cName")
IncCounter(CounterName)
%>
<script language="JavaScript">
<!--
var URL = "<% =URL %>";
function beginDownload() {
window.location = URL;
}
//-->
</script>
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.
This page uses JavaScript to redirect the client's browser rather then the Response.Redirect used in the previous examples.