i'm making a form and i would like to make the submitting PHP page accessible only if the form was submitted, preventing custom requests to my PHP page.
This is my 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>
and then my 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);
}
?>
The problem is that if i do a custom post request without submitting the form in my previous page, the data is submitted to the db, that means that an automated program could just put whatever it wants in my db... How can i prevent this?
Thanks!