this is my first time posting on stackoverflow for a couple of years, so please excuse me if this isn't the correct posting format.
I'm trying to create a simple php registration and login script using mysqli. I have not yet added prepared statements to protect myself from SQL injection attacks as I want to learn the basics of submitting and receiving data from a mysql database first.
I have created a registration script that uses the password_hash function. This is completely working and is submitting to my mysql database perfectly.
However, i'm having problems with the password_verify function. I cannot get it to seem to work. If i don't use the password_hash or password_verify function in my register.php or login.php, the login works perfectly.
Although when using password_verify and password_hash together i cannot get the code to work.
Here's the code for my login.php:
<?php
include('database_connect.php');
if(isset($_POST["submit"])) {
$email = $_POST['email'];
$password = $_POST['password'];
$query = mysqli_query($conn, "SELECT * FROM users WHERE email='$email'");
$row = mysqli_fetch_array($query);
$encrypted_password = $row['password'];
if($row == 1) {
if(password_verify($password, $encrypted_password)) {
session_start();
$_SESSION['email'] = $email;
header("Location: index.php");
exit;
} else {
echo "Incorrect email or password";
}
} else {
echo "Email cannot be found, please sign up for an account";
}
}
?>
Here is the code for my register.php:
<?php
include('database_connect.php');
if(isset($_POST["submit"])) {
$firstName = $_POST['firstName'];
$lastName = $_POST['lastName'];
$email = $_POST['email'];
$password = $_POST['password'];
$confirmPassword = $_POST['confirm_password'];
$encrypted_password = password_hash($password, PASSWORD_DEFAULT);
$sql = "INSERT INTO users (firstName, lastName, email, password) VALUES ('$firstName','$lastName', '$email', '$encrypted_password')";
if ($conn->query($sql)) {
echo "<span>Account created succesfully, please <a href='login.php'>log in</a></span>";
} else {
echo "<span>There was an error, please contact site administrator</span>";
}
}
?>
I have removed the registration form validation so that the code is easier to read. If anybody could point me to the right direction or help me out, i would gladly appreciate it.
I'm pretty certain it has something to do with password hash, but i'm not sure and after countless attempts i cannot get it working.
All the best.