2010-01-20 17 views
1

我需要在我正在构建的web应用程序中实现一个简单的PHP代理(其基于Flash并且目标服务提供程序不允许对其crossdomain.xml文件进行编辑)构建一个简单的php url代理

任何PHP大师能提供关于以下2个选项的建议吗?另外,我认为,但我不确定,我还需要包含一些标题信息。

感谢您的任何反馈!

选项1

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

选项2

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

if ($content !== false) 
{ 

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

哇。我认为我的眼睛受伤了。请转义'$ _GET'参数。 – Franz 2010-01-20 17:04:26

回答

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(); 

请注意,此代码没有经过测试,这只是给你一个起点。

+0

真的很感激反馈!将作为一个起点,让你知道它是如何发展的。 我不是一个php编码器,但会认为有很多情况下,需要这种类型的代理... – 2010-01-20 21:25:43

+0

只是1语法错误我发现,缺少关闭括号上的if(!.... line 我还发现需要在我的服务器上安装一个缺少的HTTP_Request2 php类 – 2010-01-20 22:49:05

+0

您也使用'new HTTP_Request2($ _ GET ['path'])''这是否有一些内部验证,或者是否应该添加类似内容, – Franz 2010-01-20 23:13:17

0

这是另一种使用卷曲的解决方案 任何人都可以评论?

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