SQL PHP HTML ASP JavaScript articles and free scripts to download
 

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
FilePath=Server.MapPath("count.txt")
Set FSOobj = Server.CreateObject("Scripting.FileSystemObject")
if FSOobj.fileExists(FilePath) Then
Set OpenFileobj = FSOobj.OpenTextFile(FilePath, Reading)
count = OpenFileobj.ReadLine
Response.Write count
OpenFileobj.Close
Set OpenFileobj = Nothing

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

Set OpenFileobj = FSOobj.OpenTextFile(FilePath, Writing)
count=count+1
OpenFileobj.Write(count)
OpenFileobj.Close
Set OpenFileobj = Nothing
Else
Response.Write "File does not exist"
End if
Set FSOobj = Nothing
%>
Further readings
Server.MapPath to convert virtual path to physical path of files and directory.
FileSystemObject:Checking file or folder exist by using File system object .
Getting the file name from full path of the current running script in ASP.
Getting the file extension and file name by using FSO
Deleting file by using DeleteFile in FSO
Getting the Last updating and creation date and time of a file.
Listing all files present inside a folder using FileSystemObject
Reading data from a text file using File System Object
Writing data to a text file using File System Object
Read & Writing data to a text file: Developing a page hit counter
Appending data to a text file: Storing emails in a text file
 
Scripts
PHP
JavaScript
All ASP Tutorials
File Management
MapPath
FileSystemObject
append
name
Updating Time
Listing all files
Reading file
Writing file
Read & Write
File type
Delete File
Popular Tutorials
Managing two drop downs
ASP Tutorials
Date and time
Declaring array
Form in ASP
Server.MapPath
Date Time & MSSQL
Select Query
File System Object
Subscribe
Submit your email address and receive article and product notifications. Your email is safe with us.