【问题标题】:How to display exec output into a text area如何将 exec 输出显示到文本区域
【发布时间】:2014-11-23 15:00:25
【问题描述】:

我有一个页面,点击按钮时,会调用一个执行以下操作的 javascript 函数:

 <?php $i = exec('/etc/init.d/iptables status'); ?>

  document.getElementById('txtArea').innerHTML += <?php echo $i; ?>;

但是文本区域是空的,
任何建议或解决方案?

【问题讨论】:

  • 粘贴您现在编写的 HTML 标记和 JS 代码。
  • txtArea 是什么元素?一个div?输入?还有什么?
  • 你为什么要连接你的txtArea
  • 我想把iptables的规则添加到文本区,想法是把值显示到HTML中的一个显示项中,文本区或者输入文本都没有关系
  • Duvdevan 如何将 exec 的输出放入文本区域?

标签: javascript php linux exec


【解决方案1】:

PHP 输出为 HTML:

<textarea><?php echo shell_exec('/etc/init.d/iptables status'); ?></textarea>

PHP 输出到 HTML,但在 Textarea 元素上以 Javascript 值更新的形式:

<textarea id="txtArea">...</a>
<script>
document.getElementById("txtArea").value = "<?php echo shell_exec('/etc/init.d/iptables status'); ?>";
</script>

但我怀疑,你想要这个。

所以,终于又来了,但现在用一个按钮作为 JS/jQuery Ajax Get 请求的触发器:

<textarea id="txtArea">...</a>

<input type="button" id="txtAreaUpdateButton" value="Update Textarea"/>

<script>
$(document).ready(function() {
   $('#txtAreaUpdateButton').click(function() {
      $.ajax({ 
          type: "GET",
          url: "http://www.yoursite.com/request.php",//get response from this file
          success: function(response){ 
            $("textarea#txtArea").val(response); // update textarea with response
        }
      });
   });
});
</script>

这里的“request.php”包含:

<?php echo shell_exec('/etc/init.d/iptables status'); ?>

留意报价。它们通常是错误的根源。

【讨论】:

  • 感谢@Jens-André Koch 的解决方案,我在 shell_exec 中遇到了问题,如果我编辑 request.php 以使用 exec 命令,它会返回最后一行,但使用 shell_exec 页面是空白的,即使我补充一点。我检查了 sudeors 和 php.ini 中的 safe_mode 已关闭。shell_exec 是否还有其他安全要求?
  • 嗯,也许是权限问题。看一下文件夹和文件的权限。 PHP用户是否能够执行命令? Ì如果您不知道用户运行:shell_exec('whoami');,然后使用 chown 更改 perms。 -- 请尝试启用错误报告:error_reporting(E_ALL); ini_set('track_errors',1); ini_set('display_errors',1);,然后查看日志文件以跟踪 shell_exec() 问题。你也可以用一个简单的 ls exec 进行测试,比如shell_exec('ls -la /home');,只是为了看看你是否得到了执行。
  • Jens 我在 iptables 目录上为用户 apache 使用了 chown。我也激活了错误报告,但没有添加任何日志。 ls 被执行。对于 iptable,除了 chown -R 之外还有什么我错过的吗?
  • 奇怪。 echo shell_exec('iptables --list');呢?
  • 试试这个chmod +x /etc/init.d/iptables
猜你喜欢
  • 2012-03-22
  • 2015-04-25
  • 1970-01-01
  • 2015-04-15
  • 2013-08-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多