我有一个简单的 cmd.php 页面来运行我使用 shell_exec()输入的命令并显示输出。
但是,使用无效参数( / usr / bin / php -z )调用PHP会显示PHP的用法:
用法:php [-q] [-h] [-s] [-v] [-i] [-f] php [args ...]
等...
我附上了几张图片来表明我的意思。
PHP -v 不会产生预期的输出
PHP -z 显示PHP的用法
有什么想法吗?
修改
cmd.php
<?php
if ( isset ( $_POST['submit'] ) ) :
$response = shell_exec ( escapeshellcmd ( stripslashes ( $_POST['cmd'] ) ) );
endif;
?><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<style type="text/css">
pre#response { border: 1px solid #e0e0e0; padding: .5em; }
</style>
<title>Command</title>
</head>
<body>
<form action="cmd.php" method="post">
<p><input type="text" name="cmd" id="cmd" value="<?php echo @htmlspecialchars ( stripslashes ( $_POST['cmd'] ) ); ?>" size="50" />
<button type="submit" name="submit" id="submit" value="Submit">Submit</button>
</p>
</form>
<?php
if ( isset ( $response ) ) :
?>
<pre id="response"><?php
if ( empty ( $response ) ) :
echo 'No response.';
else :
echo htmlspecialchars ( $response );
endif;
?></pre>
<?php
endif;
?>
</body>
</html>
答案 0 :(得分:1)
shell_exec()仅返回已写入已执行进程的stdout的字符,但不返回stderr。请尝试redirecting stderr to stdout,以便将错误消息存储在$ response中。
<?php
define('REDIRECT_STDERR', 1);
if ( isset ( $_POST['submit'] ) ) :
$cmd = escapeshellcmd ( stripslashes ($_POST['cmd']) );
if ( defined('REDIRECT_STDERR') && REDIRECT_STDERR ) :
$cmd .= ' 2>&1';
endif;
$response = shell_exec( $cmd );
endif;
?><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<style type="text/css">
pre#response { border: 1px solid #e0e0e0; padding: .5em; }
</style>
<title>Command</title>
</head>
<body>
<form action="cmd.php" method="post">
<p>
<input type="text" name="cmd" id="cmd" value="<?php echo @htmlspecialchars ( stripslashes ( $_POST['cmd'] ) ); ?>" size="50" />
<button type="submit" name="submit" id="submit" value="Submit">Submit</button>
</p>
</form>
<?php if ( isset ( $cmd ) ) : ?>
<fieldset><legend><?php echo htmlspecialchars($cmd); ?></legend>
<pre id="response"><?php var_dump($repsonse); ?></pre>
</fieldset>
<?php endif; ?>
</body>
</html>
答案 1 :(得分:0)
请从命令行检查php使用的php.ini。我有同样的问题(没有从PHP命令行输出),尝试用php.ini-production替换当前的php.ini并且命令行php开始工作正常。似乎在最近的php版本中修改了一些配置变量(从5.3.10升级到5.4.3)。