Articles  
DevASP.NET for ASP.NET, VB.NET, XML and C# (C-Sharp) Developers Tuesday, March 03, 2009
Search Directory
ASP Directory
Xml Directory

FREE 12 month online training for ASP.NET & MS Expression Studio and a Free copy of MS Expression Web with Windows Server Purchase
ASP Hit Counter

Author: Asif

You can count the number of visitors on your site with a very simple ASP code. Create a file Count.asp with the following code and add in the header of every page useing Server Side Include(SSI).

<!--#Include File="Count.asp"-->

Count.asp creates a text file to write the numbers of current users. When a new user comes on the site, Count.asp reads old users count from text file, updates the value by one, and stores the new count value back in the text file. The new count value is also stored in a Session Variable so you can use that Session Variable on different pages to display current user count with the following code.

<%=Session("TotalCount")%>

To make sure that one user is counted only once we use IF THEN statement. When the count file is updated first time a Session variable is created which holds the current user count. When user goes to next page Count.asp file checks if Session Variable exists or not. If Session Variable exists then it will not count the user again.

<%

' Every time we count a user we will put the
' latest count value in the session variable "TotalCount"
' If Session Variable TotalCount is empty
' that mean this user is new and session variable
' But if Session Variable already has the value
' Then we will not count this user again.

If IsEmpty(Session("TotalCount"))  Then  
 Call CountThisUser
End If

' It is good practice to use Functions and Sub procedure
' Because all the variables being used in sub or function
' are automatically destroyed when Sub or Function finish
' processing the code.
' So you can use these Variables again in other functions
Sub CountThisUser()

 Dim objFSO           ' FileSystemObject
 Dim objTS            ' TextStreamObject
 
 Dim strFileName      ' Counter text File Name
 Dim intOldCount
 Dim intNewCount
  
 ' Specify the Text file to store count value
 ' Because We Set Create = True
 ' File will be Created if it does not exist
 
 strFileName = Server.MapPath("Counter.txt")
 
 Set objFSO = Server.CreateObject("Scripting.FileSystemObject")
 
 If objFSO.FileExists(strFileName)  Then
  Set objTS = objFSO.OpenTextFile(strFileName, 1)
 Else
  Set objTS = objFSO.CreateTextFile(strFileName, True)
 End If
     
 If Not objTS.AtEndOfStream  Then
  intoldCount = objTS.ReadAll
 Else
  intoldCount = 0
 End If
 
 objTS.Close
    
 intNewCount = intOldCount + 1
    
 ' Store the value of intNewCount in Session Variable
 ' So you can use it on different pages
 Session("TotalCount")= intNewCount
    
 ' Write intNewCount value back to text file
 
 Set objTS = objFSO.CreateTextFile(strFileName, True)
 objTS.Write intNewCount
 
 objTS.Close
 
 Set objFSO = Nothing
 Set objTS = Nothing
End Sub
%>

Here is the code of a simple ASP page which display the User Counts. Count.asp file is added to this page with Server Side Include and Session Variable is used to display the current user count.


File: DisplayCount.asp

<HTML>
<HEAD>
<TITLE>Display user Count</TITLE>
<!--#Include File="Count.asp"-->
</HEAD>
<BODY>
<Center>
<H1>Welcome to DevASP.com</H1>
<H2>You are User Number<%=Session("TotalCount")%></H2>
</Center>
</BODY>
</HTML

Disclaimer - Privacy
© 2002-2012 DevASP.com