Learn how to make a hit counter in ASP.
A hit counter is an essential part of a site to know how many people are coming to site. Here is an easy way to make a counter. All you need is access to ASP and be able to give a write access to a directory. We're going to save the amount of hits in a .txt file so that we won't need to worry about database connections.
Here is what we have to write in English:
- Call the FSO (File System Object) to open it for reading. If count.txt does not exist, the FSO will create it.
- Use the On Error Resume Next function to disregard an error should ASP recieve one in the case that count.txt is empty.
- Read the file to a variable.
- Add one to the variable.
- Close the file, then Open it for writing.
- Write the file in Overwrite mode ("2").
- Close the file.
- Display the number of hits.
- Clear all instances of FSO
<%
Set FSO = Server.CreateObject("Scripting.FileSystemObject")
Readfile = Server.MapPath ("count.txt")
Set File = FSO.OpenTextFile (Readfile, 1,true)
On Error Resume Next
Read = File.Readall
Count = Read
Count = Count + 1 'increments
File.Close
Writefile = Readfile
Set File = FSO.OpenTextFile (Writefile, 2)
File.Write("" & Count & "")
File.Close
Response.write("" & Count & "")
Set FSO = Nothing
%> |






