Simple Hit Counter in ASP

Wednesday, June 07, 2006

These days I'm trying my hand on .Net technologies (ASP, VB, C#) and some other web scripting. So, would frequently post some scripts for you to learn. If you have any problem in understanding these scripts, write a comment on the post and I'll surely reply to it.

So, here is a script of a simple hit counter programmed in ASP:


<%


dim hits
dim filename
dim confile
dim myserver
dim filepath
dim maxhits

set hits = server.CreateObject("scripting.filesystemobject")
filepath = server.MapPath("1")
filepath = mid(filepath,1,len(filepath)-1)
filename = server.MapPath("hits.txt")

set confile = hits.OpenTextFile(filename,1,true)
if not confile.atendofstream then
myserver = confile.readline
end if

if len(myserver)=0 then myserver="0"

if cint(myserver) > cint(Application("hits")) then
Application("hits") = myserver
else
myserver = Application("hits")
end if

Application.Lock
Application("hits") = Application("hits") + 1
Application.UnLock

Response.Write "visted: " & Application("hits")

set tfile = hits.CreateTextFile(filepath+"hits.txt",true)
maxhits = Application("hits")
tfile.writeline(maxhits)
tfile.close
set tfile=nothing

%>

4 comments:

Gursharan Singh said...

How can I use this to record hits on my blog?

Gurpreet Singh Modi said...

blogger doesn't support asp hosting... so u wud not be able to use dis directly on ur blog... i suggest u to try, remotely hosted scrips like statcounter.com or Google Analytics, www.google.com/analytics

Gursharan Singh said...

I was expecting this reply. Seems you are doing good work on the scripting end. Keep it up! but remember, eat things inside out, the domain you are working on is going to reap huge benefits if you are thoroughly clear with the things.

Gurpreet Singh Modi said...

Yes, I'll eat the language inside out... Thanx for ur skillful advice... Keep them coming in future too...

Post a Comment