PHP : Text File Hit Counter

3 Mar


Create an empty text file called counterlog.txt and
upload to the same directory as the page you want to
count hits for.

Add this line of code on your page:
<?php include “text_file_hit_counter.php”; ?>

// Open the file for reading
$fp = fopen(“counterlog.txt”, “r”);

// Get the existing count
$count = fread($fp, 1024);

// Close the file

// Add 1 to the existing count
$count = $count + 1;

// Display the number of hits
// If you don’t want to display it, comment out this line
echo “<p>Page views:” . $count . “</p>”;

// Reopen the file and erase the contents
$fp = fopen(“counterlog.txt”, “w”);

// Write the new count to the file
fwrite($fp, $count);

// Close the file



Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: