Dorset Holiday Cottages
Self Catering Cottages in Dorset UK

You are visitor Hit Counter

 

You are visitor 

<% fp = Server.MapPath("aspcount.txt") Set fs = CreateObject("Scripting.FileSystemObject") Set a = fs.OpenTextFile(fp) ct = Clng(a.ReadLine) if Session("ct") = "" then Session("ct") = ct ct = ct + 1 a.close Set a = fs.CreateTextFile(fp, True) a.WriteLine(ct) end if a.Close Response.Write ct %> var str = "<%=ct%>"; var begin = ""; for(x = 0;x

 

 

You are visitor Number 515961 to this page! (text)

 

You are visitor Number to this page! (image using Javascript)

This page demonstrates how to create an ASP hit counter. ASP uses some objects for creating/reading/writing to files on the server, and if you can do that, you can create a hit counter. The Server's FileSystem object is used to access files. The TextStream object is used to read and write to them. On the original ASP page which generated the one you see, there is an ASP tag in the space between the words "Number" and "to" above. The code appears below:

<%
fp = Server.MapPath("aspcount.txt")
Set fs = CreateObject("Scripting.FileSystemObject")
Set a = fs.OpenTextFile(fp)
ct = Clng(a.ReadLine)
if Session("ct") = "" then
Session("ct") = ct
ct = ct + 1
a.close
Set a = fs.CreateTextFile(fp, True)
a.WriteLine(ct)
end if
a.Close
Response.Write ct
%>

Note that the first thing I did was to open the file for reading, and read the value in it, converting it to an integer. The second block of code checks to see whether the user is coming back to the page during the same session. It checks for a Session variable called "ct." If the variable isn't present, it creates it. Then it increments the value from the text file, closes it, opens it for writing, and writes the new value to it. The final block of code closes the file and writes the value to the page.

The second example uses the value in a Javascript function to create an "image-based" display.

var str = "<%=ct%>";
var begin = "<img src=\"images/dg";
var middle = ".gif\" width=\"16\" height=\"21\" alt=\"";
var last = "\">";
for(x = 0;x < str.length; x++) {
str1 = str.charAt(x);
document.write(begin + str1 + middle + str1 + last);
}

There are 10 images in the images folder, called "dg0.gif" through "dg9.gif." Each image is a digit with the same value as its' identifier number in the file name. This function creates a string from the variable created in the ASP script above it (see the ASP tag inside the quotes?). It then creates several string variables which are used to construct the image tag. It loops through the string, one letter at a time, and displays the string in image form on the page.

And it's as simple as that!