Library tutorials & articles

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
     getValue.Close
%>

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
     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 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%>."

Comments

  1. 16 Dec 2003 at 21:16

    How can I put all the code into the webpage?

  2. 29 Sep 2003 at 21:43
    This script is using VBScript.
  3. 11 Dec 2002 at 18:25

    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.

  4. 11 Dec 2002 at 15:15

    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

  5. 11 Dec 2002 at 14:35

    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://usernameassword@upload.ntlworld.com in Windows Explorer or Internet Explorer and select a file and view it's properties, I can not change the Permissions of the file (any file, including the Counter.txt file). I get the error :-


    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.

  6. 11 Dec 2002 at 13:30

    Code:
    Request.ServerVariables("Path_Translated")
    is a built in VBScript function. if basically gives you the path of the page on the server (NOT the URL, the actual path where it is stored in the computer which is acting as the server...)


    "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

  7. 10 Dec 2002 at 19:04

    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
        website
    name
        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.

  8. 09 Dec 2002 at 07:02

    yeah, post the errors here so we can see what the problem is


    (is the counter.txt file granted read AND write access???)

  9. 09 Dec 2002 at 02:31

    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)?

  10. 09 Dec 2002 at 02:30

    looks like ASP;VBScript not JScript as JScript ASP Pages look uglier than that...

  11. 08 Dec 2002 at 08:28

    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.

  12. 01 Jan 1999 at 00:00

    This thread is for discussions of ASP Web Counter.

Leave a comment

Sign in or Join us (it's free).

Paul Freeman-Powell 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 familiar ...
AddThis

Related discussion

Related podcasts

  • Scott Guthrie

    Scott catches up with Scott Guthrie in an interview covering Ajax, Asp 2.0, extender controls, CSS adapters and more.

Events coming up

We'd love to hear what you think! Submit ideas or give us feedback