2010-07-12 90 views
0

我有一個簡單的cmd.php頁面來運行命令我輸入使用shell_exec()並顯示輸出。PHP - 從命令行調用.php文件不會產生輸出

  • PHP 運行的CGI
  • 進入 「PHP -v」,大多數的命令只顯示 「內容類型:text/html的」,然後將當前網頁的HTML源。
  • 但是,調用PHP帶有無效參數(的/ usr/bin中/ PHP的-z)示出了脈動熱管的使用:

    用法:PHP [-q] [-h] [-s] [-v ] [-i] [-f] PHP [參數...]

    等...

我添加了一組圖片來說明我的意思。

PHP不會產生預期的輸出

PHP -v doesn't produce expected output

PHP -z -v顯示PHP的使用

PHP -z shows PHP's usage

任何想法?

編輯

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

預期產量是多少?我們可以看看** cmd.php **嗎? – Anax 2010-07-12 08:04:13

+0

它應該顯示版本號...我發佈了源代碼。 – 2010-07-12 08:08:28

+0

您的FCGI或CGI是否通過包裝腳本在chroot中運行? – Borealid 2010-07-12 08:22:05

回答

1

了shell_exec()只返回已寫入執行過程的標準輸出,而不是標準錯誤的字符。嘗試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> 
0

請檢查從命令行使用的php.ini。我有同樣的問題(從PHP命令行沒有輸出),試圖用php.ini生產和命令行php取代當前的php.ini開始工作正常。看來有些配置變量在最近的php版本中被修改(從5.3.10升級到5.4.3)。