We're building a brand new version of the site, and we'd love to hear your ideas
Members
Technology Zones
IBM Learning Center
Articles
Hosted By
Info
|
Rated
Read 43,423 times
Contents
Related Categories
ASP Web Counter - Reading & writing
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%>."
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 with various web-based technologies on the way. He now has extensive experience of PHP, MySQL, XHTML, CSS, DHTML, JavaScript, Flash, ActionScript and Active Server Pages. During his school years, he quickly became known amongst friends as "the one who can fix computers", a reputation which earned him a year-long post working for the Computing Services Department at the University of Liverpool, where he is currently studying for a degree in Modern European Languages.
Paul's experience of web site/application design and management, along with his all-round computer software/hardware expertise led him to found his business - caeus.com (see below) - which currently employs three other staff-members, offering web site design, technical support services and data recovery, among other services.
Paul is a Christian and also a keen musician - in his spare time he plays the drums both at his church and in various bands and other music groups, as well as working as an Administrator on this web site. Paul tries not to sound too pretentious whilst writing about himself in the third person.
 (New web site currently under construction - contact me for a preview!)
Comments
-
Posted by csifrancais on 16 Dec 2003
How can I put all the code into the webpage? -
Posted by Ashlye on 29 Sep 2003
This script is using VBScript.
Posted by DjJazzy on 11 Dec 2002
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.
Posted by paulfp on 11 Dec 2002
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.tx...
Posted by DjJazzy on 11 Dec 2002
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 co...
|
Search
Code Samples
New Members
|