我是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脚本:
search.html
<html>
<body>
<form action="search.php" method="post">
keyword: <input type="text" name="keyword"><br>
<input type="submit">
</form>
</body>
</html>
php脚本:search.php
<html>
<body>
<?php shell_exec('find /var/www -Name "keyword"') $_POST["keyword"]; ?>
</body>
</html>
这应该可以做到:
<?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命令相同的操作。
然而,您必须始终对任何用户输入进行消毒,来自外部世界的一切都是邪恶的。。
明白了:
<?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 />";