I created a forgot password link and everything works(password resets, token is set back to " ", password is properly hashed) but when I try to login with my new password, my script login.inc.php is not able to recognize the new password. I also get no errors whatsoever. If anyone can teach me what I have done wrong, I would really appreciate it.
reset-password.php:
<?php
session_start();
include 'database/login-dbh.php';
if (isset($_GET["email"]) && isset($_GET["token"])) {
$email = $_GET['email'];
$token = $_GET['token'];
$sql = "SELECT id FROM user WHERE email='$email' AND token='$token'";
$result = mysqli_query($conn, $sql);
$num_rows = mysqli_num_rows($result);
if ($num_rows > 0) {
$str = "0123456789qwertyuiopasdfghjklzxcvbnm";
$str = str_shuffle($str);
$str = substr($str, 0, 35);
$password = password_hash($str, PASSWORD_DEFAULT);
$sql = "UPDATE user SET password='$password', token='' WHERE email='$email'";
mysqli_query($conn, $sql);
echo "Your new password is: $str";
}
}else{
header("Location: forgot-password.php?error=token+not+found");
exit();
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Yahbang</title>
<link rel="stylesheet" type="text/css" href="stylesheet/forgotpassword-style.css">
</head>
<header>
</header>
<body>
</body>
</html>
login.inc.php:
<?php
session_start();
include '../database/login-dbh.php';
$email = $_POST['email'];
$pwd = $_POST['pwd'];
$sql = "SELECT * FROM user WHERE email= ? ";
$stmt = mysqli_prepare($conn, $sql);
mysqli_stmt_bind_param($stmt, 's', $email);
mysqli_stmt_execute($stmt);
$result = mysqli_stmt_get_result($stmt);
$row = mysqli_fetch_assoc($result);
if(password_verify($pwd, $row['password'])) {
$_SESSION['id'] = $row['id'];
$userID = $row['id'];
header("Location: ../index.php?id=$userID");
exit();
} else {
header("Location: ../index.php?error=login");
exit();
}
?>