Max Gunter
Max Gunter

Reputation: 213

Download - max connections per user?

I have a php-script thats allows user to download large files with download resume.

This works fine but I have limited the downloadspeed with php-code to 200kb/s per connection.

So, Mozillas Firefox download manager makes only one connection and the speed is around 200kb/s but Free-Download-Manager or JDownloader makes 2 to 4 connections and so, then the downloadspeed is (200Kb/s * 2 or 4) = 400 to 800kb/s.

How can I stop this and allow only one connection for every user to download this file?

Upvotes: 7

Views: 1286

Answers (1)

Baba
Baba

Reputation: 95141

A. I think the first thing for you is to disable Content-Range ..

14.16 Content-Range

The Content-Range entity-header is sent with a partial entity-body to specify where in the full entity-body the partial body should be applied. Range units are defined in section 3.12.

Download manager can download a single fine in 2 or multiple connections because of range .. if you disable this both download resume or multiple connections can not be made on a single file. they would make every request to the file start from the beginning

Example

LoadModule headers_module modules/mod_headers.so 
Header set Accept-Ranges none 
RequestHeader unset Range

You should also look at 14.35.1 Byte Ranges

B. Introduce Download Sessions .

You can generate a uniqid id for each download and serve it via PHP page. If the download is still active or has been requested before you just exist the page

Example

$realFile = "test.pdf";
$fakeFile = uniqid("file");

$uniqid = isset($_REQUEST['id']) ? $_REQUEST['id'] : null;
if (empty($uniqid) || strlen($uniqid) < 20 || !ctype_xdigit($uniqid)) {
    die("Die! Die! Die! Stolen URL");
}
$memcache = new \Memcache();
$memcache->connect('localhost', 11211);

$runtime = (int) $memcache->get($uniqid);

if ($runtime) {
    die("Die! Die! Die! You Multiple Down loader");
} else {
    header("Expires: Mon, 26 Jul 1997 05:00:00 GMT\n");
    header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
    header("Content-Transfer-Encoding: binary");
    header("Content-disposition: attachment; filename=$fakeFile.pdf"); //
    header('Content-type: application/pdf');
    header("Content-length: " . filesize($realFile));
    readfile($realFile);
    $memcache->set($uniqid, 1);
}

Simple Client

$url = "a.php?id=" . bin2hex(mcrypt_create_iv(30, MCRYPT_DEV_URANDOM));
printf("<a href='%s'>Download Here</a>",$url);

It would output something like

<a href='a.php?id=aed621be9d43b0349fcc0b942e84216bf5cd34bcae9b0e33b9d913cccd6e'>Download Here</a>

You also need map each id to a particular file ...

Upvotes: 7

Related Questions