【问题标题】:How to use exec shell with PHP and MySQL?如何在 PHP 和 MySQL 中使用 exec shell?
【发布时间】:2014-04-09 11:49:03
【问题描述】:

我想通过连接到数据库在 PHP 代码中使用 exec shell。我曾使用过这段代码,但它不起作用!我不知道可能是什么问题,所以这是我的代码:

if(isset($_POST['submit4']))

{
$results = shell_exec("cat /var/log/apache2/access.log | grep" . $_POST['key'] . "| sed s/'^.*apache2'/''/g | sort | uniq -c | sort -nr | cat ");
echo $results ;

}

else if(isset($_POST['submit5']))
{
mysql_connect('127.0.0.1',"root"," ") or die("erreur de connexion au serveur");
    mysql_select_db("lastnline");
        $sql='Select * from motclef';    
    $res=mysql_query($sql);
           while($row=mysql_fetch_array($res))

    {
$results = shell_exec("cat /var/log/apache2/access.log | grep" . $row['nom'] . "| sed s/'^.*apache2'/''/g | sort | uniq -c | sort -nr | cat ");
echo $results ;

    }       
}

submit4 工作得很好,但是 submit5 不工作:/

【问题讨论】:

  • 我猜有问题,因为它doesn't work
  • 我可以在你的服务器上执行任意命令。
  • 当您发现自己在使用“不起作用”这个短语时,请停下来,转而陈述 (a) 您预期会发生什么,以及 (b) 实际发生了什么。 “不起作用”可能是最没有帮助的错误报告 ;)
  • 请问这段代码是否正确: $results = shell_exec("cat /var/log/apache2/access.log | grep" . $row['nom'] . "| sed s/'^.*apache2'/''/g | sort | uniq -c | sort -nr | cat ");回声 $results ;

标签: php mysql shell


【解决方案1】:

但永远不要将输入值直接填充到 shell 命令中。使用函数escapeshellarg()

$key = escapeshellarg($_POST['key']);

【讨论】:

  • 感谢帮助,但我的代码可以正常工作,而且非常好,这只是我的数据库字段之间的错字,而不是:grep"。$row['nom']。"我应该使用 grep"。$ row['word'] ." 因为在我的表中我有 2 个字段 id + word :) 并且非常感谢!我知道你不是编程保姆,因为你没有帮助我,我独自发现了我的错误!
  • 完美的第一步 ;)
  • @user3445535,请务必接受 hek2mgl 的建议 - 您的代码目前容易受到严重的安全问题的影响。
  • 为什么你能解释更多!!
  • 因为有人可能会在POST['key']中发送foo ; cat db.config.php
猜你喜欢
  • 2015-12-28
  • 1970-01-01
  • 2015-10-02
  • 2010-12-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-02-16
相关资源
最近更新 更多