Library tutorials & articles
ASP Web Counter
- Introduction
- Reading & writing
- Administering your counter
- Conclusion
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.
Related articles
Related discussion
-
Help to Call ASP function from onclick event in HTML to pass an array
by vka (0 replies)
-
Binary Studio | software development outsourcing Ukraine
by shane124 (4 replies)
-
Variable In Vb.Net
by chia (0 replies)
-
ideas in building a captive portal
by sjranjan (2 replies)
-
How to debug classic ASP pages during AJAX calls in ASP.NET website
by andwan0 (0 replies)
Related podcasts
-
Scott Guthrie
Scott catches up with Scott Guthrie in an interview covering Ajax, Asp 2.0, extender controls, CSS adapters and more.
How can I put all the code into the webpage?
Thanks for your help Paul. I've sent NTL an E-Mail about permissions and the <% tags. I'll let you know how it goes and if I've got any more problems.
Cheers for now,
Mark.
you'll need to email ntl and ask them to change the permissions for you.
they (probably) only allow themselves to change them, cos then it's more secure and less prone to hacking.
the counter.txt file must not be read only.
check with NTL that they allow you to use the <% tags. it's conceivable (but unlikely) that they forbid them
Hi,
I run my pages by uploading them to my Remote Server, then I use Internet Explorer to visit the page, example - http://homepage.ntlworld.com/mfj/......... and then the ASP page.
I can execute code with the the <script language="VBScript"> </script> tags but I can not run code with the <% %> (scriptlet) tags.
Maybe the Request command may work when I get the scriplets to work.
I have also noticed, when I view my files on the Remote Server using ftp://username
An error occurred changing the permissions on the file or folder on the FTP Server. Make sure you have permission to change this item.
Details:
550 CHMOD not allowed on page.asp
All the files permissions are set to :-
Owner - Read Checked, Write Checked, Execute UnChecked.
Group - Read Checked, Write UnChecked, Execute UnChecked.
All Users - Read Checked, Write UnChecked, Execute UnChecked.
Maybe this is the problem. If so, how can I change the Permissions.
My OS is Windows XP, and I am the Computer Administrator.
Thank you for your time and patience.
Mark.
"websitename" and "recentvisitor" are just variable names that I've thought up. the idea is that you change "websitename" to whatever your web site's called. and "recentvisitor" you might as well leave the same.
i personally reckon it's easier to program ASP pages in notepad, missing out the "middle man" [dreamweaver] altogether.
one question about your scripts not running: Are you definetely running them from the web server on your PC, so it actually "requests" and "parses" them?? or are you just opening the asp page in internet explorer?
if you're using NT/XP you can use IIS, for 9x it's PWS you'll use.
then you'll access the pages from http://localhost/page.asp (you don't have to be online for this to work....)
forgive me if you knew all that already... i was just making sure
I am using DreamWeaver MX. I can't seem to run VBScript that starts and finshes with the <% and %> symbols. I can run VBScripts that start with <script language="VBScript"> and finish </script>. I also can not get the REQUEST command to work. As I am new at web development, to use this code, do I create a HTM page or a ASP page. If I have to create a ASP page, I take it that the page has to be a ASP VBScript.
May I also ask the correct format for (examples if possible):-
PathTranslated
websitename
recentvisitor
My upload address is :-
http://upload.ntlworld.com/
The address that is used to browse my web site is :-
http://homepage.ntlworld.com/mfj1967/.......and then the home page.
I do know quite a bit about Visual Basic for Applications, but VBScript is slightly different.
Please can you help.
Regards,
Mark.
yeah, post the errors here so we can see what the problem is
(is the counter.txt file granted read AND write access???)
oopos... didnt read the 'Couldnt get it to work' part till now...
whats wrong? Be more precise, what errors, outputs etc are you facing? What Server are you using? Eg. Microsoft IIS, Linux, Unix? Is it local server or is it on on the web?(Eg a host)?
looks like ASP;VBScript not JScript as JScript ASP Pages look uglier than that...
Just a quicky,
I'm just getting into Web Publishing, and I know all there is to know about Visual Basic.
My question is :-
Is the code your using VBScript or JavaScript, It looks very much like VBScript, but I could be wrong. The reason I ask is because I can't seem to get the code to work. I created a ASP VBScript page and added the code. Maybe I'm doing something wrong.
Please Advise, Thank you.
Mark.
This thread is for discussions of ASP Web Counter.