Don't you just hate when that happens? Halfway through writing the tutorial, Internet Explorer crashes. I'm back in beloved Gentoo now, so we'll try this again, shall we?
Here's the code we'll be working on for our counter:
Code:
<%
Set FS=Server.CreateObject("Scripting.FileSystemObject")
Set RS=FS.OpenTextFile(Server.MapPath("counter.txt"), 1, False)
counter=RS.ReadLine
RS.Close
counter=counter+1
Set RS=FS.OpenTextFile(Server.MapPath("counter.txt"), 2, False)
RS.Write counter
RS.Close
Set RS=Nothing
Set FS=Nothing
%>
Okay, let's get started. The <% and %> just mark the beginning, and end of our code.
Set FS=Server.CreateObject("Scripting.FileSystemObject ")
This line creates a "File System Object" using FS. But, for the sake of simplicity, it lets us access files. If you want, you can change FS to something else, I just use it because it's simple and easy to rememer (FS - FileSystemobject).
Set RS=FS.OpenTextFile(Server.MapPath("counter.txt"), 1, False)
This sets things up so we can open our text file that will keep track of how many hits we have. If you decide to change FS like I mentioned you could before, do the same here, as well as anywhere else it's mentioned further on.
Again, you can change RS here to whatever you want, it just makes the next few parts of code smaller, and easier to edit.
The 1 on the end of this line means "Open the file, just so we can read from it". 2 would mean "Open the file so we can read from it, and write things to it". but we only want to read from it right now, so it's good to use 1.
counter=RS.ReadLine
Now, this line reads the number from the counter text file, and stores it in a value "counter". We can use this later on to display how many hits your page has had.
RS.Close
Now, we close the file, we're done reading from it.
counter=counter+1
Now, this code will run every time somone visits your website. So, assuming it's running now and has come this far, we can pretend somone has just visit your website. So, we want to add a hit to your counter. This line does that, by adding 1 to the number we stored in "counter".
Set RS=FS.OpenTextFile(Server.MapPath("counter.txt"), 2, False)
Here we open our counter text file again, but this time we open it to write to, so we can save the new hit.
RS.Write counter
Write the new number in counter to the text file.
RS.Close
Close the file again, we're done!
Set RS=Nothing
Set FS=Nothing
These two lines are sort of good practice, but not really needed. They just make FS and RS useless unless we set them again with those big lines at the top.
Now, wasn't that fun? Don't celebrate yet, we need to put the counter in out page! Here's how we do that:
Code:
<html>
<body>
This page has been visited <%=counter%> times.
</body>
</html>
That's it. Just add =counter to wherever you want the number of hits to your page displayed. The <% and %> are there again, because we have to let the server know that there's some code in the middle of that webpage.
Last notes:
I would advise putting the counter code at the very top of the page you want a counter on, that way it will update your hits before te number of hits is displayed. See below for a full example.
In order for this to work, you will need write permissions for scripts on your website. You can check with your websites host if you aren't sure if you do or not.
I also suggest creating the counter.txt (or whatever you decide to call it) and then giving it a number to begin with... say, 0, then upload that.
Now you can celebrate.
Code:
<%
Set FS=Server.CreateObject("Scripting.FileSystemObject")
Set RS=FS.OpenTextFile(Server.MapPath("counter.txt"), 1, False)
counter=RS.ReadLine
RS.Close
counter=counter+1
Set RS=FS.OpenTextFile(Server.MapPath("counter.txt"), 2, False)
RS.Write counter
RS.Close
Set RS=Nothing
Set FS=Nothing
%>
<html>
<body>
This page has been visited <%=counter%> times.
</body>
</html>