ASP Web Counter

Reading & writing

Reading from the file in ASP

You must use the TextStream object in order to read information from a file. To do this, create the object as follows:

Dim sPath, filesys, count, getValue, update, twohrs

  sPath = Request.ServerVariables("Path_Translated")
  sPath = Left(sPath,InStrRev(sPath,"\")) & "counter.txt"

  Set filesys = CreateObject("Scripting.FileSystemObject")

     Set getValue = filesys.OpenTextFile(sPath,1,0)
     ' get the current value
     count = getValue.ReadLine
If Request.Cookies("website_name")("recentvisitor") <> "yes" Then
        ' increment by 1 before displaying, 'cos they're a newbie
        count = Int(count) + 1
     End If
     ' close file

In the code above, the current value of the counter is retreived and stored in a variable called "count". Then, check to see if the user has a cookie (meaning they've been recently so shouldn't be counted a second time). If the user doesn't have a cookie, the value of "count" is incremented.

Incrementing the value of the number stored in the text file

If the user had a cookie, then the number was retreived... and that was all that was done. The value was stored in a variable called "count", which will be used to display the value later. However, if they are new, then you will want to put the value up. In fact, you already did in the code above if they were a new visitor. Now, we're going to save it in the text file. Add this code below what you've already got.

If Request.Cookies("website_name")("recentvisitor") <> "yes" Then
  ' only update the value in the text file if they're a newbie

  ' overwrite old text file with new one
  Set update = filesys.CreateTextFile(sPath)

     ' put new value in text file

  ' give them a cookie to make sure it doesn't count them more than once
  Response.Cookies("website_name")("recentvisitor") = "yes"
  ' make it expire in 2 hours' time
  twohrs = DateAdd("h", 2, Now)
  Response.Cookies("website_name").Expires = twohrs

End If

The above code is only executed if the user is a new user, since they don't have the cookie. You see the "WriteLine" statement? That uses the variable "count" from the code in section two. This WILL have been incremented, because *exactly* the same condition is checked for, so it is now okay to use this number. Instead of actually changing the value in the text file, we overwrite the text file with one containing the new number. Then... we give the user that cookie to stop the process happening more than twice in two hours. (2 hours is a good time as some free-phone ISPs disconnect after this long).

Displaying the value

Before displaying the value, it's worth noting that you can make the number look nicer by inserting commas (or dots, depending on the regional settings) into the number:

  ' put a comma in the number
  count = FormatNumber(count, 0, 0, -1, -1)

Then you can display the number like this: "You are visitor number <%=count%>."

You might also like...


About the author

Paul Freeman-Powell

Paul Freeman-Powell United Kingdom

Paul has been a keen web developer since the age of 11, creating a variety of web sites over the years, which gradually increased in quality as well as complexity as he learned and became famili...

Interested in writing for us? Find out more.


Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“C++ : Where friends have access to your private members.” - Gavin Russell Baker