通过 shell_exec 的意外输出

Unexpected output via shell_exec

我想写一个后台任务。所以我写了一个基本脚本来查看 shell_exec 的工作原理脚本如下:

<?php
$op = shell_exec("php -v");
echo $op;
echo "back to the test.php";
?>

输出应该是 php 版本信息,但它打印出以下意外输出:

back to the test.phpback to the test.phpback to the test.phpback to the test.phpback to the test.phpback to the test.phpback to the test.phpback to the test.phpback to the test.phpback to the test.phpback to the test.phpback to the test.phpback to the test.phpback to the test.phpback to the test.phpback to the test.phpback to the test.phpback to the test.phpback to the test.phpback to the test.phpback to the test.phpback to the test.phpback to the test.phpback to the test.phpback to the test.phpback to the test.phpback to the test.phpback to the test.phpback to the test.phpback to the test.phpback to the test.phpback to the test.phpback to the test.phpback to the test.phpback to the test.phpback to the test.phpback to the test.phpback to the test.phpback to the test.phpback to the test.phpback to the test.phpback to the test.phpback to the test.phpback to the test.phpback to the test.phpback to the test.phpback to the test.phpback to the test.phpback to the test.phpback to the test.phpback to the test.phpback to the test.phpback to the test.phpback to the test.phpback to the test.phpback to the test.phpback to the test.phpback to the test.phpback to the test.phpback to the test.php

它打印我当前页面内容 59-60 次,有时 content-type:text/html 我尝试使用另一个 php 文件输出是相同的

然后我尝试了 linux 命令,比如 date,它运行得很好 请帮我解决这个问题我不知道哪里出了问题?

我也尝试了 php 的绝对路径,但没有区别,还使用了其他 php 函数,如 system、exec 等

编辑注意: 我已将 exec 函数更改为 shell_exec 因为我放错了这个我在下面显示的输出来自 shell_exec

更新 我还没有得到问题的解决方案,但得到了问题的原因,脚本中没有任何问题,这是因为服务器。我已经写信给服务器的支持中心,但没有得到回应所以我认为这个问题已经结束

exec 函数的语法是

string exec ( string $command [, array &$output [, int &$return_var ]] )

所以它是第二个参数$output,它将被命令的输出填充。它仍然会 return array not version as string。您需要解析该数据以获取版本。

exec("/usr/bin/php -v", $out);
print_r($out);

我还建议在 exec 函数中使用 php 的完整路径。

我认为 exec 和那些功能在您的 php.ini 中被禁用。您可以通过

查看
if(function_exists('shell_exec')) {
    echo "shell_exec is enabled";
} else {
    echo "shell_exec is disabled";
}

打开 php.ini 并导航至 disable_functions

部分

如果下面列出了 shell_exec,请将其删除。

然后重新启动 apache / php 处理程序。

此外,如果启用了安全模式,则此功能将不可用。您需要禁用它。