Bac*_*alo 1 php proxy php4 proxy-classes
我需要在我正在构建的Web应用程序中实现一个简单的PHP代理(基于Flash并且目标服务提供程序不允许编辑其crossdomain.xml文件)
任何php专家可以提供以下2个选项的建议吗?此外,我认为,但我不确定,我还需要包含一些标题信息.
感谢您的任何反馈!
选项1
$url = $_GET['path'];
readfile($path);
Run Code Online (Sandbox Code Playgroud)
选项2
$content .= file_get_contents($_GET['path']);
if ($content !== false)
{
echo($content);
}
else
{
// there was an error
}
Run Code Online (Sandbox Code Playgroud)
首先,永远不要只包含基于用户输入的文件.想象一下如果有人像这样调用你的脚本会发生什么:
http://example.com/proxy.php?path=/etc/passwd
然后讨论问题:你代理什么样的数据?如果有任何类型,那么你需要从内容中检测内容类型,然后传递它,以便接收端知道它得到了什么.如果可能的话,我建议使用类似于梨子的HTTP_Request2或类似的东西(参见: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();
Run Code Online (Sandbox Code Playgroud)
请注意,此代码尚未经过测试,这只是为了给您一个起点.