Your Own Web Counter With ASP - Creating the Counter File (Page 2 of 3 )
This counter works by storing a number in a text file and then reading or updating it accordingly. So, create a text file called "counter.txt" somewhere on your web server. Make sure the file isn't read only (you'll probably have to email your provider and ask them to grant it write access or login via FTP and change it.) Put the value "0" [zero] in the text file, with no spaces before or after it.
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
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 getValue.Close %>
In the code above the current value of the counter is retrieved and stored in a variable called "count". Then, the code checks to see if the user has a cookie (meaning they've been to our site 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 retrieved... and thatís all there is to 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 implement the value. We're now 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 update.WriteLine(count) update.Close
' 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 a string 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%>."
Administering your counter You could just leave it at that... you have a fully functioning counter. However, you may want to alter the value in it (hopefully only for legitimate reasons!!). To do this, build yourself a little administration page, called, say, "counter.asp".
First of all you need to create the relevant objects, like this:
<% Dim sPath, filesys, getValue, update, count sPath = Request.ServerVariables("Path_Translated") sPath = Left(sPath,InStrRev(sPath,"\")) & "counter.txt" Set filesys = CreateObject("Scripting.FileSystemObject") %>
Then put the following code where you want the form to appear in the page:
<% If Request.ServerVariables("Content_Length") > 0 Then ' change the value here Else Set getValue = filesys.OpenTextFile(sPath,1,0) ' get the current value count = getValue.ReadLine ' close file getValue.Close
' put a comma in the number count = FormatNumber(count, 0, 0, -1, -1) %> The current value is <%=count%>. Use the form below to change it. <br><br> <form action="counter.asp" method="post"> authorisation code: <input type="password" name="password" size="20" class="text"> <br> new value: <input type="text" name="newvalue" size="20" class="text"> <br> <input type="submit"> </form> <% End If %>
This checks to see if anything has been submitted to the page. First time round, nothing will have been, so the form is shown. Notice that the action of the form is the same page, "counter.asp". Also, you'll want to password protect this feature so that Joe Bloggs from the street can't mess around with your counter!
Now we're going to construct the code for what happens when the form is submitted. Insert it where I have put the comment "change the value here":
<% If Request.Form("password") <> "your_password" Then Response.Write "<font class=""critical"">You are not authorised to change the counter!</font></td></tr></table>" ElseIf IsNumeric(Request.Form("newvalue")) <> True Then Response.Write "<font class=""critical"">You can only put in numeric values!</font></td></tr></table>" Else ' overwrite old text file with new one Set update = filesys.CreateTextFile(sPath) ' put new value in text file update.WriteLine(Request.Form("newvalue")) update.Close
If Err.Number = 0 Then Set getValue = filesys.OpenTextFile(sPath,1,0) ' get the current value count = getValue.ReadLine ' close file getValue.Close
' put a comma in the number count = FormatNumber(count, 0, 0, -1, -1)
Response.Write "<font class=""critical"">The counter was updated successfully.</font><br><br>" & vbNewLine & " The new value is <b>" & count & "</b>.</td></tr></table>" Else Response.Write "<font class=""critical"">There was an error.</font></td></tr></table>" End If End If %>
This checks to see if you have the correct password. Since this is a feature that will only be used by one person, and it's not something majorly important like government blueprints, a simple method of password authentication is used. Note that this would be inappropriate for anything which needs to be more secure, but this is a simple application and I want to avoid overkill.
The code should be pretty self-explanatory. It makes sure you're allowed to change the counter and that you're putting in a proper number. Then it uses the same principal as previously to change the value in the counter to the new value you've just put in. If all has gone well, it then looks again (this is necessary, otherwise the old value will be displayed... simply incrementing that proves nothing) to confirm the new value and produce a nice message saying it was successful. If not, it informs you of the error.
Making sure you don't count yourself This is extremely simple. All you need to do is make a page that will set you a different cookie, telling the web site that you are an administrator. Then, when putting the counter value up on the main page, add that as another condition this criteria must be met in order for the number to be increased (you'll have to add that to *both* places where it sets for the "recentvisitor" cookie).