检查“exec”是否有效已禁用

发布于 2024-09-27 09:15:30 字数 38 浏览 0 评论 0原文

PHP 中是否有任何函数可以用来检测 exec 函数是否可用?

Is there any function in PHP that I can use to detect whether or not the exec function is available?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

空城缀染半城烟沙 2024-10-04 09:15:30
<?php
function exec_enabled() {
  $disabled = explode(',', ini_get('disable_functions'));
  return !in_array('exec', $disabled);
}
?>

编辑:根据 Ziagl 的评论修复了爆炸。

<?php
function exec_enabled() {
  $disabled = explode(',', ini_get('disable_functions'));
  return !in_array('exec', $disabled);
}
?>

EDIT: Fixed the explode as per Ziagl's comment.

拥有 2024-10-04 09:15:30

下面的函数更加健壮。它处理函数名称之间有 0 个或多个空格的 disabled_functions 值,检查 suhosin 补丁的黑名单设置,覆盖 safe_mode,并存储答案以供将来参考。

function is_exec_available() {
    static $available;

    if (!isset($available)) {
        $available = true;
        if (ini_get('safe_mode')) {
            $available = false;
        } else {
            $d = ini_get('disable_functions');
            $s = ini_get('suhosin.executor.func.blacklist');
            if ("$d$s") {
                $array = preg_split('/,\s*/', "$d,$s");
                if (in_array('exec', $array)) {
                    $available = false;
                }
            }
        }
    }

    return $available;
}

The following function is more robust. It deals with the disabled_functions value having 0 or more spaces between function names, checks the suhosin patch's blacklist setting, covers safe_mode, and stores the answer for future reference.

function is_exec_available() {
    static $available;

    if (!isset($available)) {
        $available = true;
        if (ini_get('safe_mode')) {
            $available = false;
        } else {
            $d = ini_get('disable_functions');
            $s = ini_get('suhosin.executor.func.blacklist');
            if ("$d$s") {
                $array = preg_split('/,\s*/', "$d,$s");
                if (in_array('exec', $array)) {
                    $available = false;
                }
            }
        }
    }

    return $available;
}
月竹挽风 2024-10-04 09:15:30

您可以在 ini 设置 disable_functions 中搜索 exec() 函数。

if( false !== strpos(ini_get("disable_functions"), "exec") ) {
 // exec() is disabled

为了完整起见,请注意 PHP 安全模式 放置了一些功能上也有限制。

You can search the ini setting disable_functions for the exec() function.

if( false !== strpos(ini_get("disable_functions"), "exec") ) {
 // exec() is disabled

Just for completeness, note that PHP Safe Mode puts some restrictions on the function too.

昵称有卵用 2024-10-04 09:15:30

您还需要检查 safe_mode 是否处于活动状态,因为如果 safe_mode 处于打开状态,则 exec 不可用

function is_exec_available() {

    // Are we in Safe Mode
    if ( $safe_mode = ini_get( 'safe_mode' ) && strtolower( $safe_mode ) != 'off' )
        return false;

    // Is shell_exec disabled?
    if ( in_array( 'exec', array_map( 'trim', explode( ',', ini_get( 'disable_functions' ) ) ) ) )
        return false;

    return true;

}

You also need to check whether safe_mode is active as exec is unavailable if safe_mode is on

function is_exec_available() {

    // Are we in Safe Mode
    if ( $safe_mode = ini_get( 'safe_mode' ) && strtolower( $safe_mode ) != 'off' )
        return false;

    // Is shell_exec disabled?
    if ( in_array( 'exec', array_map( 'trim', explode( ',', ini_get( 'disable_functions' ) ) ) ) )
        return false;

    return true;

}
や莫失莫忘 2024-10-04 09:15:30

使用在各种 SO 帖子中找到的一些技术,对安全模式、函数存在和禁用 exec 进行了单行编译。

这将在尝试运行之前检查 exec 是否可用并已启用。如果运行 exec() 并且该函数不存在或被禁用,则会生成警告。根据可能呈现给浏览器的服务器设置,并且几乎总是将一行写入日志文件=性能影响。

// Exec function exists.
// Exec is not disabled.
// Safe Mode is not on.
$exec_enabled =
     function_exists('exec')                                            &&
     !in_array('exec', array_map('trim',explode(', ', ini_get('disable_functions'))))     &&
              !(strtolower( ini_get( 'safe_mode' ) ) != 'off')
     ;


if ($exec_enabled) { exec('blah'); }

A one-line compilation of safe mode, function exists, and disabled exec using some of the techniques found on various SO posts.

This will check that exec is available and enabled BEFORE trying to run it. If you run exec() and the function does not exist or is disabled a warning will be generated. Depending on the server settings that may render to the browser and will almost-always write a line to a log file = performance hit.

// Exec function exists.
// Exec is not disabled.
// Safe Mode is not on.
$exec_enabled =
     function_exists('exec')                                            &&
     !in_array('exec', array_map('trim',explode(', ', ini_get('disable_functions'))))     &&
              !(strtolower( ini_get( 'safe_mode' ) ) != 'off')
     ;


if ($exec_enabled) { exec('blah'); }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文