|
A simple hit counter
This tutorial will show you how to build a simple hit counter. It does not
use any sql or databases and stores the hits in a text file.
Al you need to create for this script is your asp file and a text file. In
the text file, simply enter the number 0 and save it in the same directory as
count.txt. Take a look at the basic source code.
<%@ Language="VBScript" %>
<% Response.Expires= -1
Response.AddHeader "Cache-Control", "no-cache"
Response.AddHeader "Pragma", "no-cache" %>
<%
if Session("ct") = "" then
fp = Server.MapPath("db\count.txt")
Set fs = CreateObject("Scripting.FileSystemObject")
Set a = fs.OpenTextFile(fp)
ct = Clng(a.ReadLine)
ct = ct + 1
Session("ct") = ct
a.close
Set a = fs.CreateTextFile(fp, True)
a.WriteLine(ct)
a.Close
Set a = Nothing
Set fs = Nothing
else
ct = Clng(Session("ct"))
end if
%>
Now lets break it down into 3 sections.
<%@ Language="VBScript" %>
This just states that the page is a VB script page.
<% Response.Expires= -1
Response.AddHeader "Cache-Control", "no-cache"
Response.AddHeader "Pragma", "no-cache" %>
This section stops the user refreshing the page to clock up hits.
<%
if Session("ct") = "" then
fp = Server.MapPath("count.txt")
Set fs = CreateObject("Scripting.FileSystemObject")
Set a = fs.OpenTextFile(fp)
ct = Clng(a.ReadLine)
ct = ct + 1
Session("ct") = ct
a.close
Set a = fs.CreateTextFile(fp, True)
a.WriteLine(ct)
a.Close
Set a = Nothing
Set fs = Nothing
else
ct = Clng(Session("ct"))
end if
%>
This is the main section which adds the hits.
fp = Server.MapPath("count.txt")
This tells the server where to find the file. You can modify the file location
by changing count.txt. So for instance if you wanted to to
be called hitcounter.txt and in the directory db you
would use:
fp = Server.MapPath("db\hitcounter.txt")
All you have to do is alter the file path in the quote marks.
Set fs = CreateObject("Scripting.FileSystemObject")
Set a = fs.OpenTextFile(fp)
ct = Clng(a.ReadLine)
This section opens the file using FileSystemObject and reads the first line.
It then sets the varialbe ct to the amount of hits it has already
had.
ct = ct + 1
This line adds one hit to the total number of visitors.
Session("ct") = ct
a.close
This bit saves a session variable as the new click through with the new amount
of visitors and closes the text file.
Set a = fs.CreateTextFile(fp, True)
a.WriteLine(ct)
a.Close
This code creates a new textfile over the old one and adds in the new amount
of visitors to it. Then it closes the text file.
Now you have a working hit counter. All you need to do is add the hit counter
into your page:
You are a visitor number <%=ct%>!
This would display the amount of visitors. Now to save confusion, here is the
full source code for the page:
<%@ Language="VBScript" %>
<% Response.Expires= -1
Response.AddHeader "Cache-Control", "no-cache"
Response.AddHeader "Pragma", "no-cache" %>
<%
if Session("ct") = "" then
fp = Server.MapPath("db\count.txt")
Set fs = CreateObject("Scripting.FileSystemObject")
Set a = fs.OpenTextFile(fp)
ct = Clng(a.ReadLine)
ct = ct + 1
Session("ct") = ct
a.close
Set a = fs.CreateTextFile(fp, True)
a.WriteLine(ct)
a.Close
Set a = Nothing
Set fs = Nothing
else
ct = Clng(Session("ct"))
end if
%>
<HTML>
<BODY>
You are a visitor number <%=ct%>!
</BODY>
</HTML>
|