Print Page | Close Window

Needs Help With Invisible Page Counter

Printed From: Web Wiz Forums
Category: General Discussion
Forum Name: Classic ASP Discussion
Forum Description: Discussion on Active Server Pages (Classic ASP).
URL: https://forums.webwiz.net/forum_posts.asp?TID=16089
Printed Date: 30 March 2026 at 5:05am
Software Version: Web Wiz Forums 12.08 - https://www.webwizforums.com


Topic: Needs Help With Invisible Page Counter
Posted By: Misty
Subject: Needs Help With Invisible Page Counter
Date Posted: 04 August 2005 at 2:21pm
I have code for a visible page counter. I'd like to be able to create a invisible page counter that the public cannot see on the home page. Then I'd like to be able to add an administrative password protected web page that will allow me to see the page counter for the home page. Could someone please help me to write the code for two different web pages (1. Invisible page counter for the home page 2. Visible page counter for me to view in an admin area).
 
Here's the code that I have for a page counter that people can see:\
 
<%
Dim strPath, File, file2, x,liCount, j ,resfile, d, i ,fl
strPath = "D:\inetpub\wwwroot\mywebsite.com\database\counter.txt"
Set File = Server.CreateObject("Scripting.FileSystemObject")
set file2=File.OpenTextFile(strPath)
x=file2.readline
set resfile = File.createTextFile (strPath)

resfile.writeline(x+1)

liCount=Cstr(x+1)
j = Len(liCount)
d=6 - j
 

%>
<%
 
        For i=1 to d step 1
         Response.Write ("<IMG SRC='DG0.gif'>")

        Next
  For  i=1 to j step 1
   fl="DG" + Mid(liCount, i , 1) + ".gif"
  

   Response.Write ("<IMG SRC=") & (fl) & ">"

 Next
%>
 <B><font=ARIAL size="22">Visitors
      since May 18, 2003.</FONT></B>
<%
 
%>



Replies:
Posted By: dpyers
Date Posted: 04 August 2005 at 4:17pm
Here's code I use for an invisible page counter named after the page it's on. It only displays if the page is called with ?counter=y or ?counter=yes appended to the url. It can be added to any page with an include without having to mess with the internals for any particular page.

Other variations I've done have only displayed if the request came from one of my fixed ip's or if an admin cookie had been set.

<%

Function fncPageViewCounter(ByVal strTextCounterFile)

' Increments a page view text file containing 1 line with the numbers of page views on it.
' Depending upon how your host has implemented FSO, it may error out on first execution.
' Refreshing the page should clear the error

     Dim objFSO           ' FileSystemObject
     Dim objTS            ' TextStreamObject
 
     Dim strFileName      ' Counter text File Name
     Dim intOldCount
     Dim intNewCount
  
 
     strFileName = Server.MapPath(strTextCounterFile)
 
 
     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
   
   
 ' Write intNewCount value back to text file
 
     Set objTS = objFSO.CreateTextFile(strFileName, True)
     objTS.Write intNewCount
 
     objTS.Close
     Set objFSO = Nothing
     Set objTS = Nothing
     
    fncPageViewCounter = intNewCount

End Function



'Determine the page the footer is currently included in
'and strip the .asp extension to name the counter
'
'to increment the page counter for that page

dim strPageID

strPageID = Replace(Request.ServerVariables("PATH_INFO"), ".asp", "") & ".txt"

%>

<!--
Include these tags on the page so the browser doesn't
show the page from it's cache which wouldn't increment the
page counter.
    <META HTTP-EQUIV="CACHE-CONTROL" CONTENT="NO-CACHE">
    <META HTTP-EQUIV="PRAGMA" CONTENT="NO-CACHE">
    <META HTTP-EQUIV="EXPIRES" CONTENT="0">
-->

<div align="center" style="color:#000000; font-size:75%;">
<% If Lcase(Request.QueryString("counter")) = "y"  OR Lcase(Request.QueryString("TestFlag")) = "yes"  Then %>
    <p class="countertext"><%=fncPageViewCounter(strPageID)%></p>
    <% Else
        fncPageViewCounter(strPageID)
        End if
    %>
</div>



To view the counter on another page, you just have to open and read the text file - pagefilename.txt - located in the same directory as the page.




-------------

Lead me not into temptation... I know the short cut, follow me.



Print Page | Close Window

Forum Software by Web Wiz Forums® version 12.08 - https://www.webwizforums.com
Copyright ©2001-2026 Web Wiz Ltd. - https://www.webwiz.net