我需要在我正在构建的 Web 应用程序中实现一个简单的 PHP 代理(它基于 Flash,并且目标服务提供商不允许编辑其 crossdomain.xml 文件)
任何 php 专家都可以就以下 2 个选项提供建议吗?另外,我认为(但不确定)我还需要包含一些标题信息。
感谢您的任何反馈!
option1
$url = $_GET['path'];
readfile($path);
option2
$content .= file_get_contents($_GET['path']);
if ($content !== false)
{
echo($content);
}
else
{
// there was an error
}
首先,永远不要包含仅基于用户输入的文件。想象一下如果有人像这样调用你的脚本会发生什么:
http://example.com/proxy.php?path=/etc/passwd http://example.com/proxy.php?path=/etc/passwd
然后进入问题:您代理的数据类型是什么?如果有任何类型,那么您需要从内容中检测内容类型,并将其传递,以便接收端知道它正在获取什么。我建议使用 HTTP_Request2 之类的东西或 Pear 中类似的东西(参见:http://pear.php.net/package/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();
请注意,此代码尚未经过测试,这只是为了给您一个起点。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)