donglu3243 2012-08-03 08:15
浏览 20
已采纳

php5读取远程文件的有效方法

I recently upgraded from php4 to php5 and with it came the notice that all my remote php file access no longer worked. I've been doing quite a bit of research into this and I dont seem to have a clear answer as to what is the correct way to include remote urls in php5.

The first example is to include the file this way

<?php
   $data = file_get_contents("http://example.com/example.inc.php",0);
   echo $data;
?>

The 2nd is this way

<?php
   $ch = curl_init("http://example.com/example.inc.php");
   curl_exec($ch);
   curl_close($ch);
?>

and 3rd is to set in my php.ini file

allow_url_include = On      
allow_url_fopen = On

and use the good old

<?php include_once('http://example.com/example.inc.php');?>

I want to do this right and secure.

  • 写回答

2条回答 默认 最新

  • douchuo0730 2012-08-03 08:40
    关注

    All solutions are correct and there is no real difference in safety AFAIK.

    I think the difference can be summed up like this:

    The ini-settings provide the behaviour as known from prior versions. The reason why they are disabled by default is the security thread, but that is equal to all three solutions. Including remote files is a security problem, regardless of whether you control the rmeote site or not.

    file_get_contents() and the curl extension creates some overhead, since you have to buffer the content, but for php include files that is more a cosmetic thing. Their usage is slightly more complex when reading through a script. But the buffering also adds benefits: you might create a local cache for example or a checksum towards a basic plausibility check. Also a syntax check prior to execution is possible thus preventing the crash of your calling script.

    Curl is provided as a php extension. So the curl solution only works when the extension is installed, but it offers a much higher grade of freedom, much more options. If you don't require those stay with the builtin functions.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 西门子S7-Graph,S7-300
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改