I'm having some problems with my user authentication system, what am I doing wrong?
I'm trying to query the database for a password stored on there, check if the password on the database is the same as the one entered on the login page and then I'll add functionality from there.
But it just doesn't work. It repeatedly fails and prints out "Sorry, try again."
login.php
$email = $_POST['email'];
$userpass = $_POST['password'];
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$getpass = "SELECT password FROM users WHERE email = $email";
$hash = mysqli_query($conn, $getpass);
if(password_verify($userpass, $hash)) {
echo "You're in.";
} else {
echo "Sorry, try again.";
}
register.php
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error)
{
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully <br>";
$email = $_POST['email'];
$userpass = $_POST['password'];
$options = [
'cost' => 12,
];
$hash = password_hash($userpass, PASSWORD_BCRYPT, $options);
$register = "INSERT INTO users (registered_on, email, password) VALUES (CURRENT_TIMESTAMP, '$email','$hash')";
if ($conn->query($register) === TRUE)
{
echo "New record created successfully";
} else
{
echo "Error: " . $register . "<br>" . $conn->error;
}
// Closes connection
$conn->close();
I have a hunch it is to do with how I am querying the database.
note from comment: The password_verify function is a built-in PHP function