I am trying to login. If my details are corrected, l am automatically redirected to the home page. The problem is every time l click login, it displays the error message that l entered an incorrect password. This means there is an issue with how l am trying to access the hashed password in the database. To be clear l used password_hash() to enter the password into the database.
<?php
session_start();
if(isset($_SESSION['id'])){
$user_id = $_SESSION['id'];
}else {
$user_id = '';
}
$message = [];
if(isset($_POST['submit'])){
$email = trim($_POST['email']);
$pass = trim($_POST['pass']);
$user = $conn->prepare("SELECT * FROM `users` WHERE email = ?");
$user->execute([$email]);
$row = $user->fetch(PDO::FETCH_ASSOC);
if($row) {
if(password_verify($pass, $row['password'])){
$_SESSION['id'] = $row['id'];
header('location: home.php');
exit();
} else {
$message[] = "Incorrect password";
}
} else {
$message[] = "Email not found";
}
}
?>
Elvis Chimuse is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.