I made a simple register-login service with PHP and MySQL, but when I try to log in, it returns fails even though I entered the correct ID and Password.
I'm not sure but I think password_verify() is the trouble.
The structure of my database is here
idx, int(10), AUTO_INCREMENT
id, varchar(255)
password, varchar(255)
email, varchar(255)
The signup process is here
<?php
header('Content-Type: text/html; charset=utf-8');
$conn = mysqli_connect("localhost", "luminous", "***password***", "luminous");
$id = $_POST['id'];
$hashedPassword = password_hash($_POST['password'], PASSWORD_DEFAULT);
$email = $_POST['email'];
$query = "INSERT INTO member (id, password, email) VALUES('$id', '$hashedPassword', '$email')";
$result = mysqli_query($conn, $query);
if ($result === false)
{
echo "Failed to store.";
echo mysqli_error($conn);
}
else
{
?>
<script>
alert("Welcome, you've joined my service.");
// location.href = "../index.php";
</script>
<?php
}
?>
The login process is here
<?php
header('Content-Type: text/html; charset=utf-8');
$conn = mysqli_connect("localhost", "luminous", "***password***", "luminous");
$id = $_POST['id'];
$password = $_POST['password'];
$query = "SELECT * FROM member WHERE id ='$id'";
$result = mysqli_query($conn, $query);
$row = mysqli_fetch_array($result);
$hashedPassword = isset($row['password']) ? $row['password'] : false;
$passwordResult = password_verify($password, $hashedPassword);
print_r($passwordResult);
// if ($passwordResult === true)
if($passwordResult === true)
{
session_start();
$_SESSION['id'] = $row['id'];
?>
<script>
alert("Login successful.")
location.href = "../index.php";
</script>
<?php
} else {
echo "wrong!!!";
?>
<script>
alert("Login failed.");
location.href = "../index.php";
</script>
<?php
}
?>
p.s.)
I registered test account for examination, ID - lumes password - lumes email - lumes@lumes.kr. and I checked whether my database returns the right information, but It seems there's no problem with the database because it returned the exact information.
0 : 9
idx : 9
1 : lumes
id : lumes
2 : $2y$10$LSjBJGmNMlyY12RU4NTnked3i26i5o7MIWnJ8slu0Y4FWH84vb6o.
password : $2y$10$LSjBJGmNMlyY12RU4NTnked3i26i5o7MIWnJ8slu0Y4FWH84vb6o.
3 : lumes@lumes.kr
email : lumes@lumes.kr
What I can do fix this problem? I need some help.
Thank you.
(I ask for your understanding even though I am not good at asking questions...!)