Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/shell/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP-如何知道服务器是否允许shell_exec_Php_Shell_Shell Exec - Fatal编程技术网

PHP-如何知道服务器是否允许shell_exec

PHP-如何知道服务器是否允许shell_exec,php,shell,shell-exec,Php,Shell,Shell Exec,在某些服务器上,不允许PHP通过shell_exec运行shell命令。如何检测当前服务器是否允许通过PHP运行shell命令?如何通过PHP启用shell命令执行?您可以检查函数本身的可用性: if(function_exists('shell_exec')) { echo "exec is enabled"; } 顺便问一下:是否有使用“shell_exec”而不是“exex”的特殊要求 编辑#1 正如DanFromGermany指出的,您可能会检查它是否是可执行的。像这样的东西

在某些服务器上,不允许PHP通过shell_exec运行shell命令。如何检测当前服务器是否允许通过PHP运行shell命令?如何通过PHP启用shell命令执行?

您可以检查函数本身的可用性:

if(function_exists('shell_exec')) {
    echo "exec is enabled";
}
顺便问一下:是否有使用“shell_exec”而不是“exex”的特殊要求

编辑#1

正如DanFromGermany指出的,您可能会检查它是否是可执行的。像这样的东西就行了

if(shell_exec('echo foobar') == 'foobar'){
    echo 'shell_exec works';
}
编辑#2


如果上面的例子可能会产生警告,你可以用一种更合适的方式。只是。

首先检查它是否可调用,然后检查它是否未被禁用:

is_callable('shell_exec') && false === stripos(ini_get('disable_functions'), 'shell_exec');
这种通用方法适用于任何内置函数,因此您可以将其通用化:

function isEnabled($func) {
    return is_callable($func) && false === stripos(ini_get('disable_functions'), $func);
}
if (isEnabled('shell_exec')) {
    shell_exec('echo "hello world"');
}

注意使用
stripos
,因为PHP函数名不区分大小写。

如何通过PHP启用shell命令执行?
仅在根配置中。这是一个安全功能。
我的sideNo也有同样的问题。该功能已禁用,但它确实存在。“这是行不通的。@是的,你完全正确。这就是为什么我更新了我的答案,并插入了对一个不错的SO答案的引用。而且你可能还应该检查是否启用了安全模式。@ChristopherWill:,所以我没有包括它。我是不是因为没有包括它而遗漏了什么(很明显,除了啊,我甚至不知道这是不推荐的。谢谢你的更新。我想就是这样了。
function isEnabled($func) {
    return is_callable($func) && false === stripos(ini_get('disable_functions'), $func);
}
if (isEnabled('shell_exec')) {
    shell_exec('echo "hello world"');
}