So I have created typical registration/login screens for my project.
The registration part appears to be flawless, works like a charm. The password that the user inserts gets hashed into the database, so, for example if the password inserted in registration is the following:
'abcd1234' (without the single quotes)
In the MySQL database it would be stored as the following:
'$2y$10$6MvYy.59NyWsLUHYawc5d.oStB8U9RW8QfwRmMV5urbZUMEWOH7yi' (without the single quotes)
Now the problem is on the login side. When the user tries to login using 'abcd1234', he is unable to login. If he inserts '$2y$10$6MvYy.59NyWsLUHYawc5d.oStB8U9RW8QfwRmMV5urbZUMEWOH7yi' it will work, but I need the user to be able to insert the password 'abcd1234' for login and not the hashed password. I am boggled as to where I went wrong in the code.
Here is my code in the login file (I have this set up to work with a "Remember Me" checkbox):
<?php
session_start();
require 'dbcon.php';
if($_SERVER['REQUEST_METHOD'] == 'POST') {
$email = addslashes($_POST['email']);
$password = addslashes($_POST['password']);
$remember = $_POST['remember'] ?? null;
$query = "SELECT * FROM users WHERE email = '$email' && password = '$password' limit 1";
$row = query($query);
if($row) {
$row = $row[0];
$_SESSION['SES'] = $row;
if($remember) {
$expires = time() + ((60*60*24)*7);
$salt = "*&salt#@";
password_hash((time() . $salt),PASSWORD_DEFAULT);
$tokenkey = hash('sha256', (time() . $salt));
$tokenvalue = hash('sha256', ('Logged_in' . $salt));
setcookie('SES', $tokenkey.':'.$tokenvalue, $expires);
$id = $row['id'];
$query = "UPDATE users SET tokenkey = '$tokenkey', tokenvalue = '$tokenvalue' ";
$query .= " WHERE id = '$id' limit 1";
query($query);
}
header("Location: index.php");
die;
} else {
echo '<script>
window.location.href="login.php";
alert("Email or password incorrect");
</script>';
}
}
?>
And here is my code in the registration file:
<?php
session_start();
require 'dbcon.php';
if(isset($_POST['submit'])) {
$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];
$username = $_POST['username'];
$email = $_POST['email'];
$usertype = $_POST['usertype'];
$country = $_POST['country'];
$password = $_POST['password'];
$cpassword = $_POST['cpassword'];
$sql = "SELECT * FROM users WHERE username = '$username'";
$result = mysqli_query($con, $sql);
$count_username = mysqli_num_rows($result);
$sql = "SELECT * FROM users WHERE email = '$email'";
$result = mysqli_query($con, $sql);
$count_email = mysqli_num_rows($result);
if($count_username == 0 & $count_email == 0) {
if($password == $cpassword) {
$hash = password_hash($password, PASSWORD_DEFAULT);
$sql = "INSERT INTO users(firstname, lastname, username, email, usertype, country, password) VALUES('$firstname', '$lastname', '$username', '$email', '$usertype', '$country', '$hash')";
$result = mysqli_query($con, $sql);
if($result) {
echo '<script>
window.location.href="login.php";
alert("User registered successfully");
</script>';
}
}
} else {
if($count_username>0) {
echo '<script>
window.location.href="register.php";
alert("Username already exists");
</script>';
}
if($count_email>0) {
echo '<script>
window.location.href="register.php";
alert("Email already exists");
</script>';
}
}
}
?>
Any help would be greatly appreciated.
Thank you!
I tried to change this line: $password = addslashes($_POST['password']);
to this: $hash = password_hash($password, PASSWORD_DEFAULT);
and the query to this: $query = "SELECT * FROM users WHERE email = '$email' && password = '$hash' limit 1";
But it did not work.