1

構築中の Web アプリケーションに単純な PHP プロキシを実装する必要があります (Flash ベースであり、宛先サービス プロバイダが crossdomain.xml ファイルの編集を許可していません)。

次の2つのオプションについてアドバイスを提供できるPHPの専門家はいますか? また、ヘッダー情報も含める必要があると思いますが、よくわかりません。

フィードバックをお寄せいただきありがとうございます。

オプション1

$url = $_GET['path'];
readfile($path);

オプション2

 $content .= file_get_contents($_GET['path']);

 if ($content !== false) 
 {  

      echo($content);
 } 
 else 
 {  
      // there was an error
 }
4

2 に答える 2

5

まず第一に、ユーザー入力のみに基づいてファイルをインクルードすることは決してありません。誰かがあなたのスクリプトを次のように呼び出したらどうなるか想像してみてください:

http://example.com/proxy.php?path=/etc/passwd

次に、問題に進みます。どのような種類のデータをプロキシしていますか? 何らかの種類がある場合は、コンテンツからコンテンツの種類を検出し、それを渡して、受信側が何を取得しているかを知る必要があります。可能であれば、HTTP_Request2 などの Pear のようなもの ( http://pear.php.net/package/HTTP_Request2を参照) を使用することをお勧めします。アクセスできる場合は、次のようなことができます。

// First validate that the request is to an actual web address
if(!preg_match("#^https?://#", $_GET['path']) {
        header("HTTP/1.1 404 Not found");
        echo "Content not found, bad URL!";
        exit();
}

// Make the request
$req = new HTTP_Request2($_GET['path']);
$response = $req->send();
// Output the content-type header and use the content-type of the original file
header("Content-type: " . $response->getHeader("Content-type"));
// And provide the file body
echo $response->getBody();

このコードはテストされていないことに注意してください。これは出発点を示すためのものです。

于 2010-01-20T20:14:18.043 に答える
0

curl を使用した別のソリューションを次に示します。

$ch = curl_init();
$timeout = 30;
$userAgent = $_SERVER['HTTP_USER_AGENT'];
curl_setopt($ch, CURLOPT_URL, $_REQUEST['url']);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
curl_setopt($ch, CURLOPT_USERAGENT, $userAgent);

$response = curl_exec($ch);    
if (curl_errno($ch)) {
    echo curl_error($ch);
} else {
curl_close($ch);
echo $response;
}
于 2010-01-21T20:06:03.577 に答える