Here’s how you can create yourself the simplest PHP hit counter that records the number of times any resource on your website that has been accessed (visited or downloaded). It can be either a web page or a downloadable file package. The hits number will be stored in a plain text file named count.txt.
Hit counter for a web page
You have a web page at here: http://www.example.com/mypage.php and you want to display the number of times this page has been visited on the web. Put these lines in mypage.php where you want the hit count displayed:
<?php
$hit_count = @file_get_contents('count.txt'); // read the hit count from file
echo $hit_count; // display the hit count
$hit_count++; // increment the hit count by 1
@file_put_contents('count.txt', $hit_count); // store the new hit count
?>
Depending on the permissions set, you may need to manually create the text file count.txt. With most hosts, however, this snippet should automatically create the file for you. If it doesn’t work, create a text file count.txt in the same directory with mypage.php and put an 0 in it.
Download counter for a downloadable file package
To record the number of times a file has been downloaded, for instance, for a file located at here: http://www.example.com/download/pics.zip, first you need to create a PHP file named download-pics.php, place it in the /download directory and put these lines in it:
<?php
$hit_count = @file_get_contents('count.txt');
$hit_count++;
@file_put_contents('count.txt', $hit_count);
header('Location: http://www.example.com/download/pics.zip'); // redirect to the real file to be downloaded
It’s basically the same with the counter of web page hits except that download-pics.php redirects to the real file URL after recording the hit in count.txt. Similarly, you may need to create count.txt in /download directory and put an 0 in it for the first time.
Now, instead of giving the real URL of the file to your user, you would give them this URL:
http://www.example.com/download/download-pics.php
So they will download pics.zip from here instead of from the real URL or the download hit will not be recorded.
To show the number of times pics.zip has been downloaded, just put these lines in the intended place of any PHP file that’s located in the /download directory (the same directory of count.txt):
<?php
$hit_count = @file_get_contents('count.txt');
echo $hit_count;
?>
You should also read:
- PHP: Count Lines of a File and Get the Number of Lines in a Text File
- PHP: Hide the Real File URL and Provide Download via a PHP Script
- Use .htaccess to allow access only from a single HTTP referrer
- PHP: Count Words in a String
- MySQL: Add Statistics Column for the Number Count of Records from Another Table


Facebook
Twitter
Google Plus
{ 11 comments… read them below or add one }
hi, wat about for multiple fils in a folder?
Thanks, exactly what i have been looking for. But like the first guy said, what if i have to use this for multiple articles or downloads? Also i was thinking of using MySql for storing number of counts or downloads, is that possible?
Sure that is possible. But it would require some extra work. Plain files are always the easiest solution.
You can definitely do that. Just create separate count.txt files for each of the files in the directory: count_1.txt, count_2.txt, etc.
Really nice post. No matter if the blog is for business or pleasure, adding a counter to the site is a good idea.
Thanks a lot, actually i am searching for such a code for counter on my web. Thanks again, I will try it.
Hi, thank you for this script.
i cannot get this to work when i placed everthing with the correct paths
in to a sub-directory.
Any idea how that is possible?
Not bad, although this would be more efficiant using a database and MySQL, as well as having something like download.php?file=example.zip .
thanks…
Nice and simple script thank you but the only question I ask is how to add extra text to the hit counter?
echo $hit_count; <<.< I am probably just being stupid and my cold isn't helping me to concentrate all that well.
How to create counter in real time, this is using the increment, which is not real time.