【发布时间】:2020-09-22 16:06:12
【问题描述】:
我有一个 HTML 文件,里面有一个表单。当这个表单提交时,它会向 PHP 文件发送一个 POST 请求。 PHP 文件创建与 MySQL DB 的连接并更新其中的一行。
问题是任何人都可以获取此 POST 请求并将其发送到 PHP 文件同时,当 PHP 收到这些请求时,它将在数据库中执行更新并破坏数据库。
如何阻止用户发送这些请求?如何更改我的代码并使其更安全?
非常感谢!
index.html
<form action="send.php" method="post">
<input type="text" name="product">
<button type="submit">Submit and Send</button>
</form>
和...
发送.php
<?php
$DATABASE_HOST = 'localhost';
$DATABASE_USER = 'root';
$DATABASE_PASS = '';
$DATABASE_NAME = 'test';
$conn = mysqli_connect($DATABASE_HOST, $DATABASE_USER, $DATABASE_PASS, $DATABASE_NAME);
if ( mysqli_connect_errno() ) {
// If there is an error with the connection, stop the script and display the error.
exit('Failed to connect to MySQL: ' . mysqli_connect_error());
}
$prod = $_POST['product'];
$date = date('d.m.Y');
$stmt = mysqli_stmt_init($conn);
if (!mysqli_stmt_prepare($stmt, "INSERT INTO store (product, date_added) VALUES (?,?)")) {
exit('MySQL Error');
} else {
mysqli_stmt_bind_param($stmt, 'ss', $prod, $date);
mysqli_stmt_execute($stmt);
header('Location: index.html');
exit();
}
?>
我的数据库是这样的:
id | product | date_added |
--------------------------------
1 | wood | 01.01.2020 |
--------------------------------
【问题讨论】:
-
请给我们看代码
-
@nbk 对不起,我会补充。
-
同时 POST 请求究竟应该如何“破坏”数据库?
-
@gre_gor 我的意思是,例如,我可以编写一个 Python 脚本,将随机的 1000 个发布请求发送到文件。它的数据库将充满随机垃圾。
标签: php mysql security http-post form-submit