Linux PHP exec - 检查是否启用或禁用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2749591/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHP exec - check if enabled or disabled
提问by Adrian M.
Is there a way to check in a php script if exec()
is enabled or disabled on a server?
如果exec()
在服务器上启用或禁用,有没有办法检查 php 脚本?
采纳答案by nc3b
if(function_exists('exec')) {
echo "exec is enabled";
}
回答by Michael D Price
This will check if the function actually works (permissions, rights, etc):
这将检查该功能是否确实有效(权限、权限等):
if(exec('echo EXEC') == 'EXEC'){
echo 'exec works';
}
回答by Lance Cleveland
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 是否可用并已启用。如果您运行 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')) != 1
;
if($exec_enabled) { exec('blah'); }
回答by Ismael Miguel
This is some ugly code I made to detect if a function is enabled or not.
这是我为了检测某个功能是否启用而编写的一些丑陋的代码。
function is_enabled($f)
{
if($f=='ini_get')return@ini_get('a')===false;
return(($l=@ini_get('disable_functions'))===null||!is_callable($f)||!function_exists($f)||!in_array($f,array_map('trim',explode(',',$l)));
}
//Usage example:
print_r(is_enabled('str_split'));//true or null if ini_get() is disabled
回答by Duncanmoo
ini_get('disable_functions')
ini_get('disable_functions')
What you actually want to do is use ini_get('disable_functions')
to find out if it is available to you:
你真正想要做的是ini_get('disable_functions')
用来确定它是否对你可用:
<?php
function exec_enabled() {
$disabled = explode(',', ini_get('disable_functions'));
return !in_array('exec', $disabled);
}
?>
Answered on stackoverflow here: Check if "exec" is disabled, Which actually seems to come from the PHP Man page: http://php.net/manual/en/function.exec.php#97187
在此处在 stackoverflow 上回答:检查“exec”是否被禁用,这实际上似乎来自 PHP 手册页:http: //php.net/manual/en/function.exec.php#97187
Path
小路
If the above returns true (you can use exec()), but PHP can still not trigger the script there is a good chance that you have a path issue for that script, test this by doing:
如果上述返回 true(您可以使用 exec()),但 PHP 仍然无法触发该脚本,那么您很有可能该脚本存在路径问题,请通过执行以下操作进行测试:
print exec('which bash');
and then try
然后尝试
print exec('which ogr2ogr');
回答by tungpksa
I am assuming that you are running this on a linux server.
我假设您在 linux 服务器上运行它。
You could test the exec function by running the following php script:
您可以通过运行以下 php 脚本来测试 exec 函数:
exec("whoami", $ret);
echo $ret[0];
This will return the command whoami.
这将返回命令 whoami。
If it errors out, it is because the exec function could not run.
如果它出错,那是因为 exec 函数无法运行。
回答by Stergios Zg.
Example:
例子:
if(strpos(ini_get('disable_functions'),'ini_set')===false)
@ini_set('display_errors',0);
回答by Thamaraiselvam
It's a bit tricky to find exec
function available until unless checking all possibilities
exec
除非检查所有可能性,否则找到可用的功能有点棘手
1.function_exist('exec')
1.function_exist('exec')
2.Scanning through ini_get('disabled_functions)
2.扫描通过 ini_get('disabled_functions)
3.Checking safe_mode
enabled
3.检查safe_mode
启用
function is_shell_exec_available() {
if (in_array(strtolower(ini_get('safe_mode')), array('on', '1'), true) || (!function_exists('exec'))) {
return false;
}
$disabled_functions = explode(',', ini_get('disable_functions'));
$exec_enabled = !in_array('exec', $disabled_functions);
return ($exec_enabled) ? true : false;
}
This function never throws warnings unless ini_get
function not disabled.
除非ini_get
功能未禁用,否则此功能永远不会引发警告。
回答by Sinan Eldem
(Based on other responses) To check if execexists and services are running:
(基于其他响应)要检查exec 是否存在并且服务是否正在运行:
function isRunning($serviceName)
{
return exec('pgrep '.$serviceName);
}
if (@exec('echo EXEC') == 'EXEC') {
$services = [
'mysql',
'nginx',
'redis',
'supervisord',
];
foreach ($services as $service) {
if (isRunning($service)) {
echo $service.' service is running.<br>';
} else {
echo $service.' service is down.<br>';
}
}
}
// mysql service is running.
// nginx service is running.
// redis service is running.
// supervisord service is down.