PDA

View Full Version : FSO for a counter sets value back to 0.


Morgoth
06-05-2003, 05:41 AM
Hello,

I have a simple counter running on a page where I want to keep track of how many times the page has been viewed (refreshed).

The code is simple:

Dim FSO, Counter, Error, File, Hits

Set FSO = CreateObject("Scripting.FileSystemObject")
Counter = Server.MapPath("db/counter.txt")

Set File = FSO.OpenTextFile(Counter)
Hits = CLng(File.ReadLine)
Hits = Hits + 1
File.Close

Set File = FSO.CreateTextFile(Counter, True)
File.WriteLine(Hits)
File.Close

Now, as I can see, there is nothing wrong with the code, and I don't recieve errors anytime 'I' view it, but I have noticed, since I have many people viewing this page quite often, and it gets updated alot, is there some sort of FSO bug where when it gets two request at the same time (or something like it) it removes the folder, and starts over with 0?

Is there any explaination?
I have tried to fix this by adding On Error code, and seeing if there is any error with this code, it will end the application right after it checks to see if the file is there. And, I have not seen the text file ever go back to 0, but I can't be sure if my on error code works, or it hasn't be caused yet.

My guess the problem is caused by:
When someone views the page, and it reads what is in counter.txt and it also deletes the file. When it gets deleted, and someone else tries to read the file before it is remade with the new number, it errors, and remakes a new file with nothing inside (Or 1, I can't really be sure. So all the Zeros mentioned above, might be Ones).

Any help on this subject would be... helpful ;)

Thank you.

Eskimo
06-05-2003, 08:28 AM
Is this really the best way to do your hit counter, or is this testing for another purpose?

Morgoth
06-05-2003, 05:25 PM
Well, this is the only method I find fast and simple.
I would rather not involve some sort of database using access or mysql...

Roy Sinclair
06-05-2003, 06:02 PM
http://www.asp-help.com/components/ms/pagecnt_00vo.asp -- Uses a MS Control

http://www.webwizguide.com/asp/tutorials/hit_counter_tutorial.asp --- What you did.

http://www.123loganalyzer.com/webcounter/ -- A slew of counters to peruse.


At least one of those should get you a working counter. Personally I prefer to use the web server logs and a log analysis program instead of page counters. The log files give a lot more information and provide a better context.

Morgoth
06-06-2003, 12:51 AM
Listen, I have a counter... so finding a counter isn't my problem.

I just want to know why the FSO gives sets my text file back to 0 or 1...
Does no one know why?

Roelf
06-06-2003, 10:09 AM
you can test your own hypothesis by locking the application before updating the counter and then unlocking the application after it. As far as i can see, there is nothing wrong with your code. But the scenario you describe sounds right to me and you can provide that to happen with the use of Application.Lock and Application.UnLock.

Morgoth
06-06-2003, 02:10 PM
I will look into that, thank you.