Home arrow ASP arrow Page 3 - A Simple Way To Tally Your Visitors
ASP

A Simple Way To Tally Your Visitors


Hit counters are used by all types of web sites and allow us to keep an active tally of how many visits our site receives on any particular day. In this article, Borag's going to teach us how to create a simple hit counter that we can use on our personal or small business web sites.

Author Info:
By: Burag Cetinkaya
Rating: 5 stars5 stars5 stars5 stars5 stars / 19
January 20, 2002
TABLE OF CONTENTS:
  1. · A Simple Way To Tally Your Visitors
  2. · Creating our hit counter
  3. · Displaying the hit counter
  4. · Conclusion

print this article
SEARCH DEVARTICLES

A Simple Way To Tally Your Visitors - Displaying the hit counter
(Page 3 of 4 )

As mentioned earlier, we will have the option to display our hit counter as either a set of numerical images, or as plain text. We do this by passing a query string value to our hit_counter.asp script when we display it on the page (more on this soon).

Our script uses a "select case" statement to determine which method we want to use to display our hit tally:

Select Case Request.QueryString ("Mode")

Case "Text"

'Code to display text counter goes here

Case "Graphic"

'Code to display graphic counter goes here

End Select


The code for the text option of our select case statement looks like this:

'Count all visits in the database

RS.Open "SELECT COUNT (*) AS TotalVisits FROM Hits", conn, 3, 3

If Not Rs.Eof and NOT Rs.Bof Then

'Write the total number of visits

Response.Write Rs("TotalVisits")

Else

'No Records in the database

Response.Write "0"

End If


We start by executing a query and storing the results in our record set object in a single field named TotalVisits. The query uses SQL's count function to retrieve the number of rows in our hits table. If a row was returned from the query then we output the value of its TotalHits field to the using ASP's Response.Write function. If our query returned no rows, then we output 0 to the browser.

Note that the code between the else block will never actually execute unless there is an error with our query, because we have placed the code to add a record to the hits table at the top of the page.

Before we look at the output from the code I've just described, let's take a look at the code to display the tally graphically:

'Count all visits in the database

RS.Open "SELECT COUNT (*) AS TotalVisits FROM Hits", conn, 3, 3

If Not RS.Eof and NOT RS.Bof Then

'Store the total number of visits in TotalVisited Varable

TotalVisits = RS("TotalVisits")

RS.Close

'Now we will create the sequence of gif files

For I = 1 to Len (TotalVisits)

If I <> 1 Then

TotalVisits = Right (TotalVisits, Len (TotalVisits) – I)

End If



CharToReplace = Left (TotalVisits, 1)

ImgSequence = ImgSequence & "<img src='" & CharToReplace & ".gif' Border=0>"

Next

'This will write a series of <img> tags on the html page.

Response.Write ImgSequence

Else

'No Records in the database

Response.Write "<img src='0.gif' border=0>"

End If


This code is a little bit more complex that the code to display the hit tally as text, so let's run through it step-by-step:

RS.Open "SELECT COUNT (*) AS TotalVisits FROM Hits", conn, 3, 3

If Not RS.Eof and NOT RS.Bof Then

'Store the total number of visits in TotalVisited Varable

TotalVisits = RS("TotalVisits")

RS.Close


Firstly, we execute the same query that we did to get our hit tally as text, returning the number of rows in the hits table. If Access returned a row, then we get the value of its TotalVisits field into a variable and close the record set.

'Now we will create the sequence of gif files

For I = 1 to Len (TotalVisits)

CharToReplace = Mid(TotalVisits, I, 1)

ImgSequence = ImgSequence & "<img src='" & CharToReplace & ".gif' Border=0>"

Next


We loop through each character in the TotalVisits variable. Remember that variables in ASP are variants, so ASP will treat our variable as a string in this case. We grab each character in the TotalVisits variable using the mid function, save it to another variable named CharToReplace, and then remove it from the TotalVisits variable.

The signature of the mid function looks like this:

variant Mid(string s, pos p, length l)

So in our for…next loop, we retrieve each character successively. We have a variable named ImgSequence to which we append an image tag for each individual character in the TotalVisits variable. These image tags can range from 0 to 9 and are named 0.gif, 1.gif, 2.gif, etc. They are available as part of the support material for this article from the last page and should be saved into the same directory as the hit_counter.asp script. So, for example, if we had a visitor tally of 31, then the ImgSequence variable would look like this at the end of the for…next loop:

<img src='3.gif'><img src='1.gif'>

The value of the ImgSequence variable is output to the browser using the Response.Write function:

Response.Write ImgSequence

Lastly, if no records were returned from our SQL query by Access, then we display the image 0.gif, meaning that no one has visited our site:

Else

'No Records in the database

Response.Write "<img src='0.gif' border=0>"

End If


Including our counter script

Now that we have our database setup and have a full explanation of the hit_counter.asp file, it's really simply to display the hit counter on our page. We can use ASP's #include directive, to display the text counter like this:

<!--#include file="hit_counter.asp?Mode=Text" -->

Or, we can display the graphical counter, like this:

<!--#include file="counter.asp?Mode=Graphic" -->

Here's a screen shot for each method:

Displaying our hit counter as text

Displaying our hit counter as images
blog comments powered by Disqus
ASP ARTICLES

- Central Scoreboard with Flash and ASP
- Calorie Counter Using WAP and ASP
- Creating PGP-Encrypted E-Mails Using ASP
- Be My Guest in ASP
- Session Replacement in ASP
- Securing ASP Data Access Credentials Using t...
- The Not So Ordinary Address Book
- Adding and Displaying Data Easily via ASP an...
- Sending Email From a Form in ASP
- Adding Member Services in ASP
- Removing Unconfirmed Members
- Trapping HTTP 500.100 - Internal Server Error
- So Many Rows, So Little Time! - Case Study
- XDO: An XML Engine Class for Classic ASP
- Credit Card Fraud Prevention Using ASP and C...

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 

Developer Shed Affiliates

 




© 2003-2017 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials