PHP: How To Limit Download By IP Address Per Interval

This issue come to my attention after I moved one of site from my network into another host. When I’m in older host they not limited bandwidth usage but in new host they limited bandwidth usage. I got data statistics bandwidth used daily around 2-5 GB (crazy visitors *lol* text site based can consume 2-5 GB data daily). This is really annoying, I keep upgraded the hosting package to keep the site running.

3 days ago I was looking for solution for this problem, I have an idea to limit the download files to save bandwidth ?abuse? usage. I was searching on google to find sample script that can limited download to save my bandwidth. Finally I found it, and here we go…

We need MySQL database to use this script, We can limit download by IP address and giving interval how long this IP address can download again from our server. Just read this PHP snippet it’s works like a charm.

// change this value follow your hosting dan database information
mysql_select_db(‘YOURDBNAME‘, $cs_conn);



// create table if database exist
mysql_query(“CREATE TABLE IF NOT EXISTS `downloaded` (
`filepath` varchar(255) NOT NULL,
`ipadres` varchar(15) NOT NULL,
`last_access` datetime NOT NULL,
UNIQUE KEY `filepath` (`filepath`,`ipadres`),
KEY `ipadres` (`ipadres`)

// URL of file on server
$path = addslashes($_SERVER[‘REQUEST_URI’]);

// IP address of downloader
$ip = addslashes($_SERVER[‘REMOTE_ADDR’]);

// BOOLEAN, if $dl = true all download permitted
$dl = false;

$sql = sprintf(“SELECT UNIX_TIMESTAMP(last_access) last_time FROM downloaded WHERE filepath = ‘%s’ AND ipadres = ‘%s’ ORDER BY last_access DESC”, $path, $ip);
$res = mysql_query($sql);
if (mysql_num_rows($res) > 0) {
$last_xs = mysql_result($res, 0, ‘last_time’)+3600;
if ($last_xs < time()) {
mysql_query(sprintf(“REPLACE downloaded SET filepath = ‘%s’, ipadres = ‘%s’, last_access = NOW()”, $path, $ip));
$dl = true;

} else {

$sql = sprintf(“REPLACE downloaded SET filepath = ‘%s’, ipadres = ‘%s’, last_access = NOW()”, $path, $ip);
$dl = true;

if ($dl) {
$fullPath = $_SERVER[‘DOCUMENT_ROOT’].$path;
if ($fd = fopen ($fullPath, “r”)) {
$fname = basename($fullPath);
header(‘Content-type: application/octet-stream’);
header(‘Content-Disposition: filename=”‘.$fname.'”‘);
header(‘Content-length: ‘.filesize($fullPath));
header(‘Cache-control: private’);
while(!feof($fd)) {
$buffer = fread($fd, 2048);
echo $buffer;
fclose ($fd);

} else {

header(‘HTTP/1.0 503 Service Unavailable’);
die(‘Abort, your IP address’ .$ip. ‘ reached your download limit for this file’);

This script will limit download of file and giving message reach download limit from IP listed in SQL database. After 1 hour (3600 seconds) that IP can download again from server (just like rapidshare free account right?). That 3600 value can be edited to follow your needs, Example you want to limit download per IP address and you want to allow them to download again in next 24 hours so you need to change that 3600 into 86400.

Next we need to created .htaccess file to follow this rewrite rules, example :

RewriteEngine on
RewriteRule (.*)(pdf|zip|rar|png)$ /YOURDIRECTORY/dl.php [QSA]

YOURDIRECTORY is your directory name in your server, example you want to put all files to downloaded in folder named DATA you have to change YOURDIRECTORY into DATA. dl.php is the script name which contains code for limiting download if you rename it you have to change it on .htaccess to make it works.

Download this sample script and try it yourself. You might want to add some feature on this script to make it more reliable.

That’s sharing for today, have a nice day 🙂

Similar Posts:


6 thoughts on “PHP: How To Limit Download By IP Address Per Interval”

  1. There are some very great sources here and thank you for being so kind to post them here. So we can read them and give our opinion on subject.

Leave a Reply

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

Time limit is exhausted. Please reload CAPTCHA.