I have a problem with password-verify. Even though I know the password has been entered correctly I still get a false result. I use the following code to enter the hash value in mySql database: I have replaced the server login details; the input comes from a form that is created when a user scans an NFC microchip:
$servername = "localhost";
$username = "xxxxxxxxx";
$password = "xxxxxxxxx";
$database = "xxxxxxxxx"; // substitute your mysql database name
$hash = password_hash($Pass, PASSWORD_DEFAULT);
if ($UID === "0") {
echo "You have not scanned a chip to enter the registration process";
} else {
$Type = $_POST["Type"];
$Units = $_POST["UNITS"];
$LstStln = $_POST["LstStln"];
$Country = $_POST["Country"];
if (empty($_POST["eMail"])) {
$emailErr = "Email is required";
echo "Email is required";
} else {
$eMail = test_input($_POST["eMail"]);
// check if e-mail address is well-formed
if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/", $eMail)) {
die("Invalid email format. Try again.");
}
// Create connection
$conn = new mysqli($servername, $username, $password, $database);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "INSERT INTO ItsMine (UID, Password, email, Type, UNITS, LstStln, Country) VALUES ('$UID', '$hash', '$eMail', '$Type', '$Units', '$LstStln', '$Country')";
$result = $conn->query($sql);
}
}
This is the corresponding code that processes the input from this form, and which is returning false from password verify:
$servername = "localhost";
$username = "xxxxxxxxx";
$password = "xxxxxxxxx";
$database = "xxxxxxxxxx"; // substitute your mysql database name
$email = $_POST['email'];
$Pass = $_POST['Password'];
// Create connection
$conn = new mysqli($servername, $username, $password, $database);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
//get the hashed password from the database
$sql = "SELECT * From ItsMine where eMail = '$email'";
$result = $conn->query($sql);
$row = mysqli_fetch_assoc($result);
$hash = $row["Password"];
//Check password entered against the stored hash
if (password_verify($Pass, $hash)) {
$tql = "SELECT * From ItsMine where eMail = '$email'";
}