Cool PHP Download Counter / Tracker

Version 1.1

Created: October 12, 2014
Last Modified: March 8, 2021
Subscribe to Internet Tips and Tools Feed


Cool PHP Download File Counter and Tracker. This counter uses a MySQL database to store the amount of downloads for files. It also uses force download and prevents bots from downloading the files and affecting the counter.


All of your downloadable files should be in $secret_folder specified in dlcounter.php otherwise bots and users will be able to download the files directly and those downloads will not be counted. This script should not be in $secret_folder.


To stop bots from downloading your files put this honeypot for bots at the very top of your html document. The bots will follow the link and we will be able to track that they are bots and not let them download files and skew the counter results:

<a id="dlc_b" href="dlcounter.php?dlc_b=1">dlc_b</a>
<script type="text/javascript">
document.getElementById('dlc_b').style.display = 'none'; // Hide anchor from real users

Wherever you have a downloadable file on your page change the link to something like this:

<a rel="nofollow" href="dlcounter.php?file=file.exe">Download file.exe</a>

Wherever you want a counter of the number of downloads for a file on your page put this html code:

Downloaded <span id="file.exe">0</span> times.
<script type="text/javascript" src="dlcounter.php?count=file.exe"></script>
To view download stats on all files, type in browser:


Downloaded 0 times.
Please make a donation to reveal the download link.

Edit the following variables in dlcounter.php:


if (!isset($secret_folder))
	$secret_folder = "secretfolder123"; // Make this random and hard to guess. It is like a password
$purge_days = 90; // Purge bot_history and user_history that are older than this many days

One last note: The download stats also can keep track of the HTTP referer page (how the user came to your website). But in order to get the right referring page you need to put the following code at the top of your webpage. If your webpage has an .html extension instead of .php then you also need to make sure that your web server is processing php code in .html files:

	if (!isset($_SESSION['refer'])) // Only set refer if it is not set already
		$_SESSION['refer'] = $_SERVER['HTTP_REFERER'];


3/8/2021 - Version 1.1c - Bug Fix - Put inet_pton() inside of escape_string because on the outside mysql sometimes has an error because inet_pton() sometimes puts an unescaped single quote in the string.

4/1/2020 - Version 1.1b - Fixed Warning: "Warning: mysqli_affected_rows() expects exactly 1 parameter" by replacing all instances of mysqli_affected_rows() with mysqli_affected_rows($link) in the script.

3/26/2019 - Version 1.1 - All mysql functions have been converted to mysqli.

1/8/2015 - Version 1.0c - "Yahoo! Slurp" search engine was crawling the rel="nofollow" links and therefore updating the download counter. But only on some pages. Added slurp to dlcounter.php on line 180 to prevent this bot from downloading. Also updated the time from 1 hour to 24 hours for searching in bot_history on line 148 to block previous bots.

10/17/2014 - Version 1.0b - Added extra checking for bots that change their user_agent between each request. Made the download stats appear more appealing by adding user_agent and referer page to same table cell.

9/30/2014 - Version 1.0 - Cool PHP Download Counter / Tracker created.

Back to
Subscribe to Internet Tips and Tools Feed        

User Comments

There are 10 comments.

Displaying first 50 comments.

1. Posted By: Freebies - - April 3, 2015, 5:14 am
Nice Code.It really Helps.
PHP Download Counter Script

2. Posted By: Jean-Paul Zimmermann - - June 18, 2015, 6:46 am
This is really a great and professional job

Thanks a lot

3. Posted By: MASSIMO - - March 28, 2020, 3:46 pm
The file is not downloading the link file, it opens directly as text, it also gives errors

Warning: mysqli_affected_rows() expects exactly 1 parameter, 0 given in /home/XXXXX/XXX/miembros/dlcounter.php on line 242

Warning: mysqli_affected_rows() expects exactly 1 parameter, 0 given in /home/XXXXX/XXXXX/miembros/dlcounter.php on line 256
Warning: Cannot modify header information - headers already sent by (output started at /home/XXXXX/XXXX/miembros/dlcounter.php:242) in /home/cietesting/revit/miembros/dlcounter.php on line 277

Warning: Cannot modify header information - headers already sent by (output started at /home/XXXXX/XXXXXX/miembros/dlcounter.php:242) in /home/XXXXX/XXXXX/miembros/dlcounter.php on line 278

Warning: Cannot modify header information - headers already sent by (output started at /home/XXXXX/XXXX/miembros/dlcounter.php:242) in /home/XXXXX/XXXXX/miembros/dlcounter.php on line 279

4. Posted By: Jeff - - March 28, 2020, 7:28 pm

Can you send a link to a test website you have set up? Also you might try changing all the lines with mysqli_affected_rows() to this:

if (mysqli_affected_rows($link)==0)

It should be on lines 189, 242, 255 and 397.


5. Posted By: MASSIMO - - March 31, 2020, 10:10 am

Now Working perfect.



6. Posted By: Jeff - - March 31, 2020, 10:11 am
Hi Massimo,

You're welcome! Thank you for the update!


7. Posted By: Evans Ifendu - - July 1, 2020, 4:08 am
I want to use on mp3 files. Where exactly should the mp3 files be?

8. Posted By: Jeff - - July 1, 2020, 10:31 am
Hi Evans Ifendu,

There is a line near the top of the script that looks like this:

So you would create a folder on your server with any random secret name you want and then change the variable above to that folder name and then you would put the files to be downloaded in that secret folder.


9. Posted By: wpduta - - May 28, 2021, 11:58 pm
Nice Code. It really helps to my website

10. Posted By: KazMar - - June 25, 2021, 12:53 am
Your script is wonderful. Thank's for the great job