Good morning everyone,

I am currently building a website for the company where I work and I am looking to get a hit counter so I can pull some usage statistics. Our site is on the Intranet and not everyone has access to the Internet so I would rather not have to link this to an outside website.

Unfortunately, our company does not support PHP... why? No idea, but most of the counters that I have come across are all PHP supported so I am unable to use them.

All of my pages are html pages but I'm hoping there is some code I might be able to add in to do the work. We do support asp so maybe something along those lines? I would hopefully like to track multiple pages so I can see which ones are most popular. I am also hoping that the script which I will eventually use does not make the user click OK or YES everytime they enter the site to run it. I believe it was a VB script I ran before which did that and added the info to a plain text file. I don't mind having seperate text files for each site either, I can use a DB to later pull that information.

Any help would be greatly appreciated.

Thanks everyone in advance!

rcdeck

Dani AI

Generated

OP wanted a server-side intranet hit counter without PHP. 's JavaScript/cookie method will only track visits per browser (client-side) and can be cleared — useful for per-user counts but not for aggregate backend stats. 's classic‑ASP file approach is the right direction for a server-side solution; the common reasons it did not update are (a) the page was not actually being processed as ASP, or (b) the IIS worker account lacked write permission on the text file.

Two practical options that work on an intranet:

  • Keep pages as static .html and call a small ASP endpoint via an invisible image. This avoids renaming files and runs server-side code on every page load:
<img src="/counter.asp?page=/docs/intro.html" style="display:none" alt="" />
  • Implement counter.asp to increment a server-side store (preferred: a small DB table for concurrency; acceptable: a text file for tiny sites), then return a tiny static GIF so the image request succeeds. Example (replace connection string and table name as needed):
<%
' counter.asp (outline)
pg = Request.QueryString("page")
Set cn = Server.CreateObject("ADODB.Connection")
cn.Open "Provider=SQLOLEDB;Data Source=SERVERNAME;Initial Catalog=IntranetDB;Integrated Security=SSPI;"
cn.Execute "UPDATE PageHits SET Hits = Hits + 1 WHERE PagePath = '" & Replace(pg,"'","''") & "'", recordsAffected
If recordsAffected = 0 Then cn.Execute "INSERT INTO PageHits (PagePath,Hits) VALUES ('" & Replace(pg,"'","''") & "',1)"
Response.ContentType = "image/gif"
Response.WriteFile Server.MapPath("/blank.gif")
Response.End
%>

Troubleshooting and cautions: ensure Classic ASP is enabled and the script file is served as ASP (rename to .asp or map .html to ASP), grant write permission to the IIS worker identity for any file-based counter, avoid Response.Write in production if output must be invisible, and prefer a database over file I/O for reliability under concurrent access. The browser network/dev tools and IIS logs are the quickest way to confirm the counter endpoint is being hit and returning 200.

Recommended Answers

All 7 Replies

JavaScript hit counter.

HEADER SCRIPT

<SCRIPT LANGUAGE="JavaScript">
<!--
 function nameDefined(ckie,nme)
{
   var splitValues
   var i
   for (i=0;i<ckie.length;++i)
   {
      splitValues=ckie[i].split("=")
      if (splitValues[0]==nme) return true
   }
   return false
}

function delBlanks(strng)
{
   var result=""
   var i
   var chrn
   for (i=0;i<strng.length;++i) {
      chrn=strng.charAt(i)
      if (chrn!=" ") result += chrn
   }
   return result
}
function getCookieValue(ckie,nme)
{
   var splitValues
   var i
   for(i=0;i<ckie.length;++i) {
      splitValues=ckie[i].split("=")
      if(splitValues[0]==nme) return splitValues[1]
   }
   return ""
}
function insertCounter() {
 readCookie()
 displayCounter()
}
 function displayCounter() {
 document.write('<H3 ALIGN="CENTER">')
 document.write("You've visited this page ")
 if(counter==1) document.write("the first time.")
 else document.write(counter+" times.")
 document.writeln('</H3>')
 }
function readCookie() {
 var cookie=document.cookie
 counter=0
 var chkdCookie=delBlanks(cookie)  //are on the client computer
 var nvpair=chkdCookie.split(";")
 if(nameDefined(nvpair,"pageCount"))
 counter=parseInt(getCookieValue(nvpair,"pageCount"))
 ++counter
 var futdate = new Date()
 var expdate = futdate.getTime()
 expdate += 3600000 * 24 *30  //expires in 1 hour
 futdate.setTime(expdate)

 var newCookie="pageCount="+counter
 newCookie += "; expires=" + futdate.toGMTString()
 window.document.cookie=newCookie
}
// -->
</SCRIPT>

BODY SCRIPT

<SCRIPT LANGUAGE="JavaScript">
<!--
insertCounter()
// -->
</SCRIPT>

If little change, it will be what you need. Do not overpower, I'll help.

Hmm,

I placed that code on my page, though where I put the body code I have nothing showing up, and when I do load the page, at the bottom I get "Errors on page" message (or something close to that.)

I should have also mentioned, it doesnt need to be displayed on the page, just something I can see from the back end.

Very strange. Ok, I'll make of something else

JavaScript hit counter.

HEADER SCRIPT

<SCRIPT LANGUAGE="JavaScript">

corrected javascript

<script type='text/javascript'>

asp server counter

<%
on error resume next 
' Create server object
set fso = createobject("scripting.filesystemobject")
' Target text file to opened
set act = fso.opentextfile(server.mappath("asp_count.txt"))
' Read the value of the text document
' If the text document does not exist then the on error resume next
' will drop down to the next line
counter = clng(act.readline)
' Add one to the counter
counter = counter + 1
' Close the object
act.close
' Create a new text file on the server
Set act = fso.CreateTextFile(server.mappath("asp_count.txt"), true)
' Write the current counter value to the text document
act.WriteLine(counter)
' Close the object
act.Close
' Write the counter to the browser as text ::response can be quoted out or erased
Response.Write counter
%>

Would i just place that code <% through %> into the head of the page? I tried placing it there and i even created the file asp_count.txt with a 1 in it in the same directory but its not updating it :(

like php
it embeds in a html file
the file is renamed .asp
so the intranet server knows what to do with it
links to the file from outside also have to have href=filename.asp

I would go here http://www.w3schools.com/asp/default.ASP [ check the file extension :) ] for self paced tutorials
and the .asp.net and .asp forum one parent up in web development for help from code gurus

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.