I've been studying prepared statements and implemented it in my first login. I used a prepared statement for the SQL query and am wondering if this is enough to ward off most SQL injections?
Since the SQL query is parameterized and I've escaped the username I think it should be ok. The only real issue that I know of (if there are more, please let me know) is the fact that I couldn't escape the password input because of the way Bcrypt verifies it. Let me know if there is a better way of doing this.
<!Doctype html>
<html>
<head>
<title>password validation</title></head>
<body>
<fieldset>
<legend>enter password</legend>
<form action =passing.php method = "post">
<table>
<tr>
<td>Email:</td><td><input type="id" name ="id" /><br /></td>
</tr>
<tr>
<td>Password:</td><td><input type = "password" name="password" /><br /></td>
</tr>
</table>
<input type = "submit" name ="submit" value ="insert" />
</form>
</fieldset>
<br />
<?php
// connect to the server
$conn = new mysqli('localhost', 'root', '', 'social');
// check connection
if(mysqli_connect_errno()) {
exit("connection failed" . mysqli_connect_error());
} else {
echo "connection established";
}
if($_POST && isset($_POST['submit'], $_POST['password'], $_POST['id'])) {
$pass = ($_POST["password"]);
$id = mysqli_real_escape_string($conn, $_POST["id"]);
$input = $pass;
$query = mysqli_prepare($conn, "SELECT pass FROM social
WHERE email LIKE ?");
mysqli_stmt_bind_param($query,'s', $id );
mysqli_stmt_execute($query);
mysqli_stmt_bind_result($query, $id);
while(mysqli_stmt_fetch($query)) {
echo "<br />";
echo "SUCCESS at query";
if (password_verify($input, $id)) {
echo "matching pass" . header("Location: inserh.php");
} else{
echo "not a match";
}
}
}
mysqli_close($conn);
// close the connection