I have been stuck on this for some time now. I have a form that is located in index.php. The data is sent to a php file called, processuserform.php. I extract all the inputs and assign them each to their own variable. Does the following look like it is the proper way to validate and sanitize a form on Server side?
First is the form itself then the PHP file will be used to process the data sent to it.
<form method="POST" name="signup" action="php/processuserform.php">
<input id="firstname" onkeyup="validateFirstName()" placeholder="First Name" type="text" /><label id="firstnameprompt"></label>
<br><br>
<input id="lastname" onkeyup="validateLastName()" placeholder="Last Name" type="text"/>
<label id="lastnameprompt"></label>
<br><br>
<input id="Email" onkeyup="validateEmail()" placeholder="Email" type="text" />
<label id="Emailprompt"></label>
<br /><br />
<input id="Password" onkeyup="validatePassword()" placeholder="Create Password" type="password" /><label id="Passwordprompt"></label>
<br /><br />
<strong>Male</strong><input id="Gender" type="radio" name="sex" value="male">
<strong>Female</strong><input id="Gender" type="radio" name="sex" value="female">
<br /><br />
Click "Submit" if you agree to <a href="#">"Terms And Conditions"</a>
<br>
<input id="submit" onclick="return validateUserRegistration()" value="Submit" type="submit" name="submit"/>
<label id="submitprompt"></label>
<br><br>
processuserform.php
<?php
$first_name = ($_POST['firstname']);
$last_name = ($_POST['lastname']);
$email = ($_POST['Email']);
$pw = ($_POST['Password']);
$gender = ($_POST['Gender']);
// define variables and set to empty values
$first_nameErr = $last_nameErr = $emailErr = $pwErr = $genderErr = "";
$first_name = $last_name = $email = $pw = $gender = "";
if ($_SERVER["REQUEST_METHOD"] == "POST")
{
if (empty($_POST["firstname"]))
{
$first_nameErr = "Name is required";
}
else
{
$first_name = test_input($_POST["firstname"]);
// check if name only contains letters and whitespace
if (!preg_match("/^[a-zA-Z ]*$/",$first_name))
{
$first_nameErr = "Only letters and white space allowed";
}
}
if ($_SERVER["REQUEST_METHOD"] == "POST")
{
if (empty($_POST["lastname"]))
{
$last_nameErr = "Name is required";
}
else
{
$last_name = test_input($_POST["lastname"]);
// check if name only contains letters and whitespace
if (!preg_match("/^[a-zA-Z ]*$/",$last_name))
{
$last_nameErr = "Only letters and white space allowed";
}
}
if (empty($_POST["Email"]))
{
$emailErr = "Email is required";
}
else
{
$email = test_input($_POST["email"]);
// check if e-mail address syntax is valid
if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/",$email))
{
$emailErr = "Invalid email format";
}
}
if (empty($_POST["Password"]))
{
$pwErr = "Password is required";
}
else
{
$pw = test_input($_POST["Password"]);
}
}
if (empty($_POST["Gender"]))
{
$genderErr = "Gender is required";
}
else
{
$gender = test_input($_POST["Gender"]);
}
}
function test_input($data)
{
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
$hostname="this is correct";
$username="this is correct";
$password="this is correct";
$dbname="this is correct";
$db_conx = mysqli_connect($hostname, $username, $password) OR DIE ("Unable to connect to database! Please try again later.");
if(mysqli_connect_errno())
{
echo mysqli_connect_error();
exit();
}
$select = mysqli_select_db($db_conx,$dbname);
mysqli_query($db_conx,"INSERT INTO users (firstname, lastname, email, password, gender)
VALUES ('$first_name', '$last_name', '$email', '$pw', '$gender')");
mysqli_close($db_conx);
header("Location: not/important.php")
?>
Thanks all for your help. If I am sanitizing it and validating it wrong would someone mind giving me an example of how it should look using one of my inputs as an example? I could use help as this is a bit confusing. Thanks again!