检查open_basedir限制是否生效

时间:2014-01-29 09:43:19

标签: php open-basedir phpass

使用PHPass(http://www.openwall.com/phpass/)时,我收到以下警告:

open_basedir restriction in effect. File(/dev/urandom) is not within the allowed path(s)

虽然这不是一个大问题(它会落后于别的东西),但我不想发出这个警告。 PHP应用程序应该在不同的服务器上运行,一些用户可以将该路径添加到其允许的open_basedir路径,但其他用户将无法访问该配置。

我的第一个猜测是检查is_readable()的可读性,但是,我仍然收到警告。

问题:如何检查某个路径或文件是否已添加到open_basedir路径?

3 个答案:

答案 0 :(得分:3)

您可以使用ini_get() function

读取PHP指令的值
ini_get('open_basedir')

如果指令不为空,则应包含由PATH_SEPARATOR分隔的a string with one ore more paths

答案 1 :(得分:1)

您可以使用is_readable并使用@禁用此功能的警告。

如果is_readable返回false,那么您知道它不可读。

$readable = @is_readable(..);
if(!$readable) {
    ....not readable
}

答案 2 :(得分:0)

如果目录受限,

file_exists会发出警告。让我们使用它:

function isRestricted($path)
{
    // Default error handler is required
    set_error_handler(null);

    // Clean last error info. You can do it using error_clean_last in PHP 7.
    @trigger_error('__clean_error_info');

    // Testing...
    @file_exists($path);

    // Restore previous error handler
    restore_error_handler();

    // Return `true` if error has occured
    return ($error = error_get_last()) && $error['message'] !== '__clean_error_info';
}