ASP – An open application environment, it enables powerful server-side scripting.  Home Web Programming ASP Counter in ASP
Your Ad Here

Counter in ASP


Counter in ASPLearn 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:
  1. Call the FSO (File System Object) to open it for reading. If count.txt does not exist, the FSO will create it.
  2. Use the On Error Resume Next function to disregard an error should ASP recieve one in the case that count.txt is empty.
  3. Read the file to a variable.
  4. Add one to the variable.
  5. Close the file, then Open it for writing.
  6. Write the file in Overwrite mode ("2").
  7. Close the file.
  8. Display the number of hits.
  9. Clear all instances of FSO
Here it is in ASP:

<%
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
%>

Thats it! Just place the code into any file, and make sure the file as well as count.txt have read/write access on the IUSR_ account. If you are using a webhost, this access is probably already set.

Author's URL: Wilfried Schobeiri
Thank you for voting.
Rate this Materials:
Bad 
1 2 3 4 5 Excellent
print this page subscribe to newsletter subscribe to rss

Web programming � everything from the basics of visual design and architecture to the specifics of applications, graphics, and scripting. More Web Programming: Most Popular Materials | Fresh Materials | More ASP Tutorials at LearnPHP.org

Add comments to "Counter in ASP"

Only registered users can write comment

No comments yet...