ASP Web Counter

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:

<%
Dim sPath, filesys, getValue, update, count
  sPath = Request.ServerVariables("Path_Translated")
  sPath = Left(sPath,InStrRev(sPath,"\")) & "counter.txt"
     Set filesys = CreateObject("Scripting.FileSystemObject")
%>

Then, where you want the form to appear in the page, put the following:

<%
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 inapproprioate for anything which needs to be more secure, but this is a simple application and I want to avoid overkill.

You might also like...

Comments

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.

Contribute

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.

“A computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are, in short, a perfect match” - Bill Bryson