Dreamhost

Looking for somewhere to host your site? Browse our PHP web hosts section!

Home > PHP Scripts > Text file hit counter

Text file hit counter

Description

A 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.

The code


<?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($fp1024);

// 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);

?> 

Get the code

Download the file to your computer: Click here to get the file