Page hit counter by reading and writing data to file

By using our reading data to file and writing data to file techniques we have learnt so far by using FSO, we can develop a counter script. This script will store the count value in a text file. Each time the file is opened the script will read the value from the text file and then store it again by increasing it by one. We can call this as page hit counter, which will tell us how many times a page is opened.

This script uses concepts of reading data from file and writing data to file. So before developing this script it is advisable to understand these scripts by reading the respective tutorials.

The page hit counter script we will divide into two parts. First part will read the previously stored count value. Second part will add 1 to the counter value and write the new value to the text file. So while using this script for first time create one simple text file count.txt and store a value 0 inside it.

In the first part after reading the value we have stored the value of the counter in a variable count. Then we will close the file ( in reading mode ) and open the file again in writing mode. Here is the complete script to develop a simple page hit counter.

Const Reading=1
Const Writing=2
Dim OpenFileobj, FSOobj,FilePath,count
Set FSOobj = Server.CreateObject("Scripting.FileSystemObject")
if FSOobj.fileExists(FilePath) Then
Set OpenFileobj = FSOobj.OpenTextFile(FilePath, Reading)
count = OpenFileobj.ReadLine
Response.Write count
Set OpenFileobj = Nothing

'Reading of files is over , now let us start writing new count value to file

Set OpenFileobj = FSOobj.OpenTextFile(FilePath, Writing)
Set OpenFileobj = Nothing
Response.Write "File does not exist"
End if
Set FSOobj = Nothing

Be the first to post comment on this article :

Post your comments , suggestion , error , requirements etc here .

We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
©2000-2020 All rights reserved worldwide Privacy Policy Disclaimer