Simplest PHP Hit Counter or Download Counter – Count the Number of Times of Access (Page Views or File Downloads)

by Yang Yang on May 19, 2010

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;

?>
Subscribe to Kavoir: blog feed

You should also read:

Lukman September 3, 2010 at 3:23 am

hi, wat about for multiple fils in a folder?

Mayur October 18, 2010 at 6:25 pm

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?

Yang Yang October 19, 2010 at 4:27 pm

Sure that is possible. But it would require some extra work. Plain files are always the easiest solution.

Yang Yang October 19, 2010 at 4:25 pm

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.

gostats October 29, 2010 at 1:14 pm

Really nice post. No matter if the blog is for business or pleasure, adding a counter to the site is a good idea.

Ahmad Ali December 14, 2010 at 4:24 am

Thanks a lot, actually i am searching for such a code for counter on my web. Thanks again, I will try it.

Jan December 30, 2010 at 7:06 am

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?

Danny December 31, 2010 at 8:41 am

Not bad, although this would be more efficiant using a database and MySQL, as well as having something like download.php?file=example.zip .

denver May 31, 2011 at 2:20 pm

thanks…

Alan September 16, 2011 at 7:36 am

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.

Nitin Gupta December 30, 2011 at 11:52 pm

How to create counter in real time, this is using the increment, which is not real time.

LoKii March 3, 2012 at 8:10 pm

After a LOT of searching and going through tons of other downloads/scripts with their ads forced or complete spamware downloads, this script of yours is absolutely brilliant and does exactly what is should in an easy and elegant way. I had to insert the 0 to start the counter, but from there on it works like a charm.

Many thanks for sharing this :)

Long April 16, 2012 at 7:32 pm

Thanks so much.
I intented to use opencart and fixed it as I wish.
Not now.
phew!

Prerna June 16, 2012 at 7:52 am

For some mysterous reason, my counter on the specified site is incrementing when running this code:

$hit_count = @file_get_contents(‘count.txt’);
echo $hit_count;

This happens ONLY on Firefox PC & Mac (but not on Safari or IE). Meaning everytime I hit reload on Firefox with the above code, count.txt is incremented by 1. This is driving me crazy, would really appreciate your help in getting this to work right.

Prerna June 16, 2012 at 8:58 am

I fixed it by decrementing if Firefox was detected. Worked. You can moderate my post above if you like.

Mary June 26, 2012 at 11:52 pm

Thanks for this post. How do I make the number of hit show beside the file on the page.

sbo July 19, 2012 at 2:16 pm

Many thanks, It took me a while to get this to work because I had the file permissions and/or a .lck file preventing write access… so in the end I set everything to 7.

Kristian August 10, 2012 at 2:16 pm

How do I avoid the file being downloaded if the counter has reached 10??

Rikudou_Sennin August 20, 2012 at 6:56 am

Or you can use .htaccess to redirect all files to a php script, that will take the requested url, so you will have the link directly to a file :)

Yang Yang August 20, 2012 at 12:03 pm

Great idea! Thanks!

ApacheTech June 29, 2013 at 10:47 pm

Any sample code for this?

Barry October 29, 2012 at 9:17 pm

What code is used to display the count on my webpage?

nathalie December 2, 2012 at 3:37 am

thanks a lot :)))))

Monu January 30, 2013 at 5:01 am

Thank you…..this helped…..!!

Rob DeWolf March 21, 2013 at 9:11 pm

If anyone is looking to include the count on a webpage, simply add this where you want the count to appear:

?

Make sure that the correct path to “count.txt” is put before it if it’s not in the same folder of the file you put the above code in. :)

Comments on this entry are closed.

Previous post:

Next post: