8:52 am Wednesday, March 10, 2010
     
SERVER TRANSFER COMPLETE. UNDER CONSTRUCTION.
            Search About Contact Home
Hit Counter Tutorial
AaronOutpost : ASP / Tutorials / Counters / Hit Counter

Hit Counter Tutorial

Welcome to Part 1 of 4 Counter Tutorials. First we are going to cover the basic hit counter.

Counters Tutorial
  1. Create a Text Hit Counter

  2. Create a Graphic Hit Counter

  3. Create a Text or Graphic Session Hit Counter

  4. Create an Active User Hit Counter using global.asa

  Create A Text Hit Count File
Create a text file, I called mine "hit_counter.txt". In this text file, you only want to place the number 0. Then of course save it to your directory at which you are constructing your Hit Counter ASP script. 

NOTE: Do not place any other characters or number in this file!

If you are a loser and you want to cheat on how many hits you have had, you can change the 0 to any number you want. The counter will start on that number. Or, maybe you are resuming an old count. Whatever it may be, that will be the starting number.

If you have read other tutorials and they use Access databases, .inc, and or any other type of file it is the same idea as my .txt file. Just, my way is easier!




Begin writing the code:

Since I want this code to display on a page, we'll start off with simple HTML codes for a display page. Name the page anything you want with in .asp format.

<html>

<head>

<title>Hit Counter</title>

</head>

<body bgcolor="white" text="black">

<div align="center">

<h3>Hit Counter</h3>

<%

'Hit Count ASP Script Here

%>

<br>

</div>

</body>

</html>

 

 

 

Dimension the variables to be used in this script. If you are unsure why we need some of these variables, still continue to read on. They are explained in detail below.

<%

'Dimension variables to be used

Dim FileSystemObject 'File System Object

Dim TextStreamObject 'Text Stream Object

Dim FileObject 'File Object

Dim NumberVisitors 'Holds the number of visitors

Dim LoopCount 'Loop counter to display the graphical hit count

 

 

OR to Conserve Space

<%

Dim FileSystemObject, TextStreamObject, FileObject, NumberVisitors, LoopCount

 

To be able to change the text file holding the hit count we need to use the Microsoft Scripting Runtime object "File System Object". This object can be used to write and read a file. So we are going to Set our variable FileSystemObject that we created to be that scripting object.

Set FileSystemObject = Server.CreateObject("Scripting.FileSystemObject")

 

 

 

Now we will use the "GetFile" method of the "File System Object". Initialize the "File Object" with the hit count text file. To get the "hit_counter.txt" text file we need to use the physical path on the server to the file. Use the ASP Server object and the "MapPath" method to get the path and file holding the hit count.

Set FileObject = FileSystemObject.GetFile(Server.MapPath("hit_counter.txt"))

 

 

 

Once the File Object has been initialized create a "TextStream Object". 

Set TextStreamObject = FileObject.OpenAsTextStream

 

 

 

Using the TextStreamObject created above and the "ReadAll" command we will read into the "hit_counter.txt" text file to define our variable NumberVisitors . We are also using the VBScript function "CLng" to convert the text to long integer.

NumberVisitors = CLng(TextStreamObject.ReadAll)

 

 

 

Now, we need to redefine NumberVisitors since the visitor will be adding one to total. 

So "Old NumberVisitors + 1 = New NumberVisitors

If you are desperate and a loser you can change the number 1 to anything you want. If you get caught, would be quite hilarious.

NumberVisitors = NumberVisitors + 1

 

 

 

Now we have to over write the old text file with the new visitors number using the "CreateTextFile" command of the "File System Object" Again using the ASP "Server" object and the "MapPath" command.

Set TextStreamObject = FileSystemObject.CreateTextFile(Server.MapPath("hit_counter.txt"))

 

 

 

Using the "TextStreamObject" write the new hit count to the "hit_counter.txt" text file. Use the "CStr" VBScript function to convert long integer to a string.

TextStreamObject.Write CStr(NumberVisitors)

 

 

 

Reset Server Objects to free resources

'Reset server objects

Set FileSystemObject= Nothing

Set TextStreamObject = Nothing

Set FileObject = Nothing

 

 

 

There are two ways to display the hit count. Text or Graphically. Most prefer graphically but text is more reliable and quicker.

The text version is displayed simply as seen below.

'-----------------------------------------------------------
'Display the hit count as text

'-----------------------------------------------------------

%>

Text Version of Count <br>
<%=
NumberVisitors %>

<br>

<br>

 

 

Finally this finished project should look something like this! Make sure it is saved in .asp format as mentioned before and must be run on an ASP2 enabled server.

Visit Part 2 Create a Graphic Hit Counter  

<html>

<head>

<title>Hit Counter</title>

</head>

<body bgcolor="white" text="black">

<div align="center">

<h3>Hit Counter</h3>

 

<%

 

Dim FileSystemObject, TextStreamObject, FileObject, NumberVisitors, LoopCount

 

'-----------------------------------------------------------
' Get and open hit count file

'-----------------------------------------------------------

Set FileSystemObject= Server.CreateObject("Scripting.FileSystemObject")

Set FileObject = FileSystemObject.GetFile(Server.MapPath("hit_counter.txt"))

Set TextStreamObject = FileObject.OpenAsTextStream

 

'-----------------------------------------------------------

' Increment count

'-----------------------------------------------------------

NumberVisitors = CLng(TextStreamObject.ReadAll)

NumberVisitors = NumberVisitors + 1

 

Set TextStreamObject = FileSystemObject.CreateTextFile(Server.MapPath("hit_counter.txt"))


TextStreamObject
.Write CStr(NumberVisitors)

 

 

Set FileSystemObject= Nothing

Set TextStreamObject = Nothing

Set FileObject = Nothing

 

'-----------------------------------------------------------
'Display the hit count as text

'-----------------------------------------------------------

%>

Text Version of Count <br>
<%=
NumberVisitors %>

<br>

<br>

<br>

</div>

</body>

</html>


AaronOutpost : ASP / Tutorials / Counters / Hit Counter
Random_Image
News & Updates

ASP Inline Calendar 3.9 (January 5, 2008)
Future Home of News & Updates (January 5, 2008)

 

Photo Special

Schedule an appointment

Available in & around:
Centreville VA, Spotsylvania VA,
Summersville WV, & Charleston WV
.

  • NAME YOUR PRICE
    • Email your:
    • Event
    • Hours Needed
    • Photos Needed
    • Disks or Prints
    • and Price You're Willing To Pay
ASP Inline Calendar

$100 Installation Special

PROFESSIONAL INSTALLATION OF YOUR ASP INLINE CALENDAR. REDUCE THE HASSLE, LET US INSTALL YOUR ASP INLINE CALENDAR. SEND A FORMAL REQUEST FOR INSTALLATION WITH SERVER ACCESS INFORMATION. WE WILL NEED FTP ACCESS ALONG WITH CONTROL PANEL ACCESS.

Purchase A Calendar
Information
Demo
Ask A Question

User must provide ftp access to server. Installations are not guaranteed. We work with your service provider to get your calendar installed.

 

Centreville, VA      -      Spotsylvania, VA      -      Charleston, WV      -      Summersville, WV

© Copyright AaronOutpost.com 2010. All rights reserved.