hit counter without needing database | using text file


If you want to have a your owan hit counter on your pages here is simple hit counter using a .txt file. Records hits to a web page, the hit count is stored in a single text file. The total number of hits is displayed on the web page using PHP but can be disabled if desired.

 

<?php 

/**
 * 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
fclose($fp); 

// 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
fclose($fp); 

?>

2 comments

  1. MyBlog says:

    It seems too advanced and very general for me to understand.

  2. happyday67 says:

    Sorry my english not good, but I can say your article make numerous sense, and I find it very informational too. Hope you can write more of those articles within the future.

Leave a Reply to MyBlog Cancel reply

Your email address will not be published. Required fields are marked *