Completely thrown in the deep end - new to PHP and doing a simple form submission (create account page) to send to a mySQL database so apologies for the noobness of question.
I'm not sure how to properly validate and sanitize the data before sending it. But i am using a PDO and placeholders when inserting into the database so I think i have that side covered.
<form action="createaccount.php" name="form" method="post">
<input type="text" name="createUserName" id="createUserName" placeholder="User Name"><br>
<input type="password" name="passWord" id="passWord" placeholder="Password (Min 6 Characters)"><br>
<input type="password" name="confirmPW" id="confirmPW" placeholder="Confirm Password"><br>
<input type="text" name="fName" id="fName" placeholder="First Name"><br>
<input type="text" name="sName" id="sName" placeholder="Surname"><br><br>
<input type="email" name="userEmail" id="userEmail" placeholder="Email Address"><br>
<input type="submit" value="Create Account">
</form>
This is sent to a seperate php file called createaccount.php which runs a username check and if success runs a function that sends the fields into an insert function in my dbHandler class.
<?php
session_start();
include('connect.php');
$username = $_POST['createUserName'];
$password = $_POST['passWord'];
$password_confirm = $_POST['confirmPW'];
$firstname = $_POST['fName'];
$surname = $_POST['sName'];
$email = $_POST['userEmail'];
if ($dbh->checkUserName($username)) {
$_SESSION['message'] = "Username is taken, please try again";
header('Location: createAccount.php');
exit();
} else {
$dbh->createAccount($username, $password, $password_confirm, $firstname, $surname, $email);
header('Location: login.php');
exit();
};
?>
So my questions are. Should i be using
<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>
In my form action? If so. I will need to run the function on createaccount.php here yes as I cant send it to another php file?
Should I be using filter_var? And/Or Should I be using trim, stripslashes or anything in my variables im sending into the form? How do I do this? My understanding is
$name = test_input($_POST['createUserName']);
$password = test_input($_POST['passWord']); .. etc
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
Also, here is my insert function. Is this secure/written correctly?
<?php
function createAccount($userName, $pw, $confirmPW, $fName, $sName, $email) {
$sql = "INSERT INTO `room_project`.`user` (`userName`, `pass`, `confirmPW`, `fName`, `sName`, `email`) VALUES (?, ?, ?, ?, ?, ?);";
$query = $this->connection->prepare($sql);
$query->execute(Array($userName, $pw, $confirmPW, $fName, $sName, $email));
}
?>
I thoroughly appreciate any advice! Again please excuse the beginner nature of this :)
You should use PHP regex to strictly validate your input data.
Suppose you want to validate the following input fields,
then you would do something like this,
And use PDO prepare to prevent your database from any kind of SQL Injection.
From the PDO::prepare
I have made an example for you to check and valieded fields before send to server. please have a look.
Its not a complete example. just get you idea how to do the null value check by javascript.