我正在制作一个表单,我想让提交的 PHP 页面只有在提交表单时才可访问,从而防止对我的 PHP 页面的自定义请求。
这是我的 form.html:
<html>
<head>
<title>Name/Surname form</title>
</head>
<body>
<form id="form1" method="POST" action="processData.php">
Name: <input type="text" id="name" name="name"><br>
Surname: <input type="text" id="surname" name="surname"><br>
<input type="submit" value="Submit form">
</form>
</body>
</html>
然后是我的 processData.php:
<?php
if(!isset($_POST['name'],$_POST['surname'])) die;
include ("config.php");
//connect
$mysqli = new mysqli($dbhost, $dbuser, $dbpassword, $dbname); //variables from config.php
//check connection
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
if ($stmt = $mysqli->prepare("INSERT INTO name_surname_table (name, surname) values (?, ?)")) {
//bind
$stmt->bind_param('ss', $name, $surname);
//set
$name=$_POST['name'];
$surname=$_POST['surname'];
//execute
$stmt->execute();
//close
$stmt->close();
}
else {
//error
printf("Prepared Statement Error: %s\n", $mysqli->error);
}
?>
问题是,如果我在未在上一页中提交表单的情况下进行自定义发布请求,则数据将提交到数据库,这意味着自动化程序可以将它想要的任何内容放入我的数据库中......我该如何防止这?
谢谢!