2012-10-15 26 views
7

我有一個php腳本,它允許用戶下載簡歷下載大文件。Download - 每個用戶的最大連接數?

這工作得很好,但我有限的PHP代碼到每個連接200KB /秒downloadspeed。因此,Mozilla的Firefox下載管理器只有一個連接,速度約爲200kb/s,但Free-Download-Manager或JDownloader可以連接2到4個連接,所以下載速度爲(200Kb/s * 2或4 )= 400至800kb/s。

我該如何阻止並允許每個用戶只有一個連接來下載此文件?

+0

一種方法是生成會話ID,當第一次連接時會失效。 – nkr

+0

您需要以某種方式識別用戶,然後限制*每個*用戶,而不是*每個*下載。 – nalply

+0

也沒關係在PHP其唯一可能的會話ID,IP不會忽略,MySQL數據庫等,使這個..它的好,但我不得不認爲這是可能超過.htacess還是其他什麼東西? –

回答

7

一個。我想對你的第一件事情就是禁用Content-Range ..

14.16 Content-Range

的內容範圍實體頭與部分實體主體發送到指定的完整實體主體部分身體應該被應用。範圍單位在3.12節中定義。如果禁用此兩者download resumemultiple connections不能對單個文件進行

下載管理器,因爲範圍2個或多個連接下載一個很好..。他們將盡一切請求文件從頭開始

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

你也應該看看14.35.1 Byte Ranges

。介紹下載會話。

您可以爲每次下載生成一個uniqid id並通過PHP頁面提供。如果下載仍然有效或已被要求之前,你只是存在的頁面

$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); 
} 

簡單的客戶端

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

它會輸出類似

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

你還需要映射每個ID到一個特定的文件...

相關問題