Published: Wednesday, April 18, 2001
Creating an ASP-driven Page Counter for HTML Pages, Part 2
By Greg Walker
Read Part 1
In Part 1 we looked at what possible solutions we could employ to
create a page counter that would work on both ASP pages and static HTML pages. In this part we'll
examine the source code for the ASP hit counting page that does the actual work of incrementing the counter,
as well as the needed HTML to display the counter (on either an ASP or HTML page).
The Source Code for HitCounter.asp
We implemented a simple counter of our own, using text files to store the
individual counter values. With a little code modification you could
easily use one of the ASP components as your counter, or use a database
in place of the individual text files. The following is the ASP code
(HitCounter.asp) responsible for keeping count of our HTML page hits as
well as formatting the page hit counts value into digits for displaying
back on our HTML pages via the "JavaScript include" technique.
The source code for HitCounter.asp has been moved from the body of this
article to: http://www.4guysfromrolla.com/webtech/code/hitcounter.asp.html
To use a counter on one of your HTML pages simply insert the following code
into your HTML page where you want your counter to show up.
<!-- Counter code version 1.0, Do NOT alter this code! -->
<SCRIPT language=JavaScript src="http://www.customer.com/counters/client1.js">
</SCRIPT>
<img src="/counters/hitcounter.asp?c_id=client1">
<!-- End of Counter Code -->
|
[
View the live demo!]
Using HitCounter.asp
To use the example code a couple of edits will need to be made:
- In the ASP file
HitCounter.asp the value of the Constant CounterPath
will need to be changed to reflect the path on the server where you
load HitCounter.asp. This path will also be where all your counter
files will be written.
- The paths used in the
HtmlCounter.html file will need to be modified
to reflect the proper values, depending on the location of
HitCounter.asp on your server.
- On your server you will need to make a folder structure like the one
used for this code, if the example folder structure is not used, the
code will need to be modified to reflect any changes made. The folder
structure required for the example code is as follows:
\counters\ | HitCounter.asp, CounterLogo.gif |
\counters\images\digits\ | All digit GIF files (included in the ZIP file
at the end of this article)
|
- The following is brief descriptions of the example files:
HitCounter.asp | ASP code that manages all html pages page hits. |
HtmlCounter.html | Html code snippet to be inserted into html pages
where you want your counter to show up. |
CounterLogo.gif | Our counterlogo in gif format use this or create
your own (may want to use similar dimensions) |
digits.zip | Zip file of digits used in building counter
use these, or substitute with your own. |
The only drawback with using the above mentioned HTML code is that the count
displayed on your HTML page will actually lag the true page hits by one. Of
course, this could be corrected by placing the image tag before the JavaScript
include. This was not a big deal to us and we wanted our counterlogo to show
up under our counter value.
With this general idea there are many other things you could do with ASP to
make static HTML pages come alive (without having to make them into ASP pages)
Please feel free to visit the site that this article was written around at
http://www.huntguide.com. All 30 plus counters on this
site are now using a similar version of the example code.
We are also developing a counter administration page for this customer's site
to allow us to maintain and administer the counters more easily. If anyone is
interested, the code for this will be posted on our site in the near future at
http://www.allinthecode.com.
Happy Programming!
By Greg Walker
Attachments:
Download the Source Code and Support Files
View the live demo!
My Background
I work full time for a medium sized Mechanical Contractor, in their Building
Automation Group as an Engineer/Programmer. I have been in this field for over
17 years. I work after hours (nights & weekends) for my wife's web design
company. I am self taught, and have been learning and working with ASP for
about a year. I owe alot of what I've been able to learn and accomplish using
ASP, to 4Guys. I had always hoped I would someday be able to give back to such
a great resource. I have thouroghly enjoyed writing this article, and hope to
get the chance to write again. Thanks 4Guys!