Hi I am tired of doing and debugging this task. I read several similar questions in the Stackoverflow.com, but unable to find a solution. The problem is that despite using same hashing method (suppose md5, hash) for both Registering and Login, every time I try to login it shows unmatched password.
I am using 'InsertUsers' class in which there is a method named 'userLogin' to which I send two arguments (userName and userPassword) from the 'login.php' file. Before sending as an argument I encrypted the password. But It says unmatched password.
When I checked the password sent by the login.php after hashing I found this string $2y$10$./syQEE.LiMBynyanGXmeei1lk/fgZrQ/K4V6PBpjsc3nlBMh6gd6 instead of the stored string (stored by registration page) $2y$10$1vrJ3Sq19uGjkIG9YG4NmOF.
Apart from unmatched issue, every time I enter the same password the login page hashed password is seen slight different. Below is the code I used. Please help me in finding out the problem. I went through several questions in Stackoverflow, but none of them matched for my problem.
In the registration.php I encrypted like this-
$user_pass = password_hash($_POST['password'], PASSWORD_DEFAULT);
And in the InsertUsers class in my userLogin method is
public function userLogin($userName, $userPassword){
try{
$sql = "SELECT id FROM users WHERE user_name = ? AND password = ?";
$stmt = $this->connect()->prepare($sql);
$stmt->bindParam(1, $userName,PDO::PARAM_STR);
$stmt->bindParam(2, $userPassword,PDO::PARAM_STR);
$stmt->execute();
if($stmt->rowCount() > 0){
$result = $stmt->fetch(PDO::FETCH_ASSOC);
return $result['id'];
}
else{
return false;
}
}
catch (PDOException $e) {
echo 'Error, Please put correct data' .$e->getMessage();
}
}
In login.php file I encrypted the password before sending it to the above method.
if((empty($_POST['username'])) || empty($_POST['password'])){
array_push($error_message, "User Name & Password required");
}
else{
$userName = $_POST['username'];
$userPassword = password_hash($_POST['password'], PASSWORD_DEFAULT);
}
if(empty($error_message)){
$Login = new InsertUsers();
$loginCheck = $Login->userLogin($userName, $userPassword);
if($loginCheck > 0){
$_SESSION['username'] = $_POST['username'];
echo "Hello ". $_SESSION['username'];
}
else{
echo "password not matched";
}
}