【问题标题】:PHP Open_basedir restrictions on URLPHP Open_basedir 对 URL 的限制
【发布时间】:2012-02-10 11:24:48
【问题描述】:

我想问一些问题。
我有一个网络服务器 (apache2/php/debian),并且出于某些安全原因,PHP 配置了 open_basedir 选项。
我需要使用 file_get_contents() 访问 url,但我收到错误警告:file_get_contents(): open_basedir 限制生效。
我检查了 php 配置,并且 allow_url_fopen 已打开。

在开发服务器(ubuntu 10.10)中它可以正常工作,但在 debian(6.0 挤压)中它不能。有什么想法吗??

PHP 版本是 5.3.3-7+squeeze7 和 Suhosin-Patch
一个例子:

php.ini:

Open_basedir = /var/securedir/:/var/www
allow_url_fopen = On

php代码:

$a = file_get_contents("http://www.php.net");
Warning: file_get_contents(): open_basedir restriction in effect.

另一个问题是:

$b = file_get_contents("/var/securedir/file.xml")
Warning: file_get_contents(): open_basedir restriction in effect. File(/var/securedir/file.xml) is not within the allowed path(s): (/var/securedir/:/var/www)

【问题讨论】:

标签: php apache2 debian open-basedir


【解决方案1】:

为什么不直接使用 cURL 来获取内容呢?这只是几行额外的代码:

function fetch_content( $sUrl ) {

    $aOptions = array(
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_HEADER         => false
    );

    $cUrlHndl = curl_init( $sUrl );
    curl_setopt_array( $cUrlHndl, $aOptions );
    $binaryContents = curl_exec( $cUrlHndl );
    curl_close( $cUrlHndl );

    return $binaryContents;
}

因为 file_get_contents 确实存在安全风险,所以它在服务器上被禁用。您收到的警告是因为您尝试打开的路径未包含在 php.ini 设置文件中。

【讨论】:

    猜你喜欢
    • 2010-09-18
    • 2021-01-20
    • 2013-01-06
    • 1970-01-01
    • 2014-09-27
    • 1970-01-01
    • 2011-03-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多