3

ユーザーがファイルをダウンロードできる、RapidShare のようなクイック サイトを開発しています。最初に、ヘッダーを設定して使用する簡単なテストを作成しましたが、コメントセクションにダウンロードの速度を制限する方法があることがreadfile()わかりました。これは素晴らしいことです。コードは次のとおりです。

$local_file = 'file.zip';
$download_file = 'name.zip';

// set the download rate limit (=> 20,5 kb/s)
$download_rate = 20.5;
if(file_exists($local_file) && is_file($local_file))
{
    header('Cache-control: private');
    header('Content-Type: application/octet-stream');
    header('Content-Length: '.filesize($local_file));
    header('Content-Disposition: filename='.$download_file);

    flush();
    $file = fopen($local_file, "r");
    while(!feof($file))
    {
        // send the current file part to the browser
        print fread($file, round($download_rate * 1024));
        // flush the content to the browser
        flush();
        // sleep one second
        sleep(1);
    }
    fclose($file);}
else {
    die('Error: The file '.$local_file.' does not exist!');
}

しかし、私の質問は、同時にダウンロードする数を制限するにはどうすればよいですか? 一部のユーザーの IP との接続がまだ存在することを確認するにはどうすればよいですか?

ありがとう。

4

1 に答える 1

3

ユーザーはログインしていますか?セッションを使用するだけでなく、IPアドレスを追跡する場合もあります。

セッションの例を次に示します。

$_SESSION['file_downloading']==true;
$file = fopen($local_file, "r");
while(!feof($file))
{
    // send the current file part to the browser
    print fread($file, round($download_rate * 1024));
    // flush the content to the browser
    flush();
    // sleep one second
    sleep(1);
}
$_SESSION['file_downloading']=null;
fclose($file);}

次に、このすべてのコードの上に、

if(!empty($_SESSION['file_downloading'])) 

//リダイレクトを実行するか、ダウンロードレートなどを下げます。

次のオプションはIPアドレス経由です。

//http://wiki.jumba.com.au/wiki/PHP_Get_user_IP_Address
function VisitorIP()
    { 
    if(isset($_SERVER['HTTP_X_FORWARDED_FOR']))
        $TheIp=$_SERVER['HTTP_X_FORWARDED_FOR'];
    else $TheIp=$_SERVER['REMOTE_ADDR'];

    return trim($TheIp);
    }

訪問者のIPアドレスを取得し、これを日時スタンプと一緒にデータベースに保存します。次に、ファイルのダウンロードが完了したら、そのIPアドレスを削除します。データベースシステムを使用していますか?

于 2011-01-07T01:55:55.660 に答える