【问题标题】:Using HTML with PHP and shell exec to search for files使用带有 PHP 和 shell exec 的 HTML 来搜索文件
【发布时间】:2015-06-25 08:45:22
【问题描述】:

我是 PHP 的完全初学者。 我正在尝试创建一个 HTML/PHP 脚本,它将使用来自 HTML 表单的用户输入,而不是使用 php shell_exec 使用“find /var/www -Name”命令搜索具有该输入的文件。

我知道如何使用 PHP 运行一个简单的脚本,但我不知道如何使用用户输入来执行此操作... 例如:“

<?php
    if (isset($_POST['button']))
    {
         exec('test.sh');
    }
?>
<html>
<body>
    <form method="post">
    <p>
        <button name="button">Run Script</button>
    </p>
    </form>
</body>

"

这就是我已经走了多远。我决定了两个文件,一个是 HTML,第二个是 PHP 脚本:

搜索.html

<html> 
<body>
<form action="search.php" method="post">
keyword: <input type="text" name="keyword"><br>
<input type="submit">
</form>
</body>
</html>

php 脚本: 搜索.php

    <html>
<body>

<?php shell_exec('find /var/www -Name "keyword"') $_POST["keyword"]; ?>
</body>
</html>

【问题讨论】:

    标签: php html forms shell-exec


    【解决方案1】:

    这应该可以解决问题:

    <?php 
    $keywords=$_POST["keyword"];
    $result=shell_exec('find /var/www -Name "'.$keywords.'"');
    echo '<pre>'.$result.'</pre>';
    ?>
    

    注意:使用这样的命令是个坏主意,因为您直接使用用户输入,用户可以在服务器上运行任何类型的命令。

    例如,如果用户键入"; rm -rf /var/www;echo " 作为搜索,它将删除您的/var/www 文件夹的全部内容。 你最好实现一个 php 函数,它和你的 find 命令做同样的事情。

    但是,您必须始终清理任何用户输入,来自外部世界的一切都是邪恶的......

    【讨论】:

    • 谢谢,真的很有帮助。
    【解决方案2】:

    知道了:

    <?php
    $keyword=$_POST["keyword"];
    $result=shell_exec(' find /var/www -name '.$keyword.'');
    if (!isset($_POST['submit'])) { // if page is not submitted to itself echo the form
    ?>
    <html>
    <!DOCTYPE html>
    <html>
    <head>
      <meta name="viewport" content="width=device-width, initial-scale=1">
      <link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css">
    </head>
    <body>
    
    <div class="container">
    
    <div class="jumbotron">
      <h1>Search</h1> 
    
    </div>
    
    <div class="row">
    
    <div class="col-md-4">
      <title>Search</title>
    </head>
    
    <body>
    <form method="post" action="<?php echo $PHP_SELF;?>">
    Suchen:<input type="text" size="12" maxlength="500" name="keyword"><br />
    <input type="submit" value="submit" name="submit">
    </form>
    <?
    } else {
    echo " ".$result."<br />";
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-02
      • 1970-01-01
      • 1970-01-01
      • 2018-04-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多