I have a change password script which is supposed to reset a users password with the values they provide assuming they match however this script is breaking something as users are not able to login with the password they provide to the script.
I'm not sure what is wrong as I also have an add_user script which is what I use to create the user accounts. The code to generate the password (hash) is the same and the data is successfully being put into the DB so I really don't know what is causing the problem. I'm guessing it has something to do with the data being provided prior to PHP hashing it thus in the DB it looks like it all went well as it's already hashed but I'm guessing if I was storing in plaintext it wouldn't be exactly the same as what the user entered otherwise the script would be working...
I've been working on the site all day so I'm really struggling to spot the error here.
I think my script used to work as this is the first I'm noticing the issue however I don't remember making any changes to this script in particular so cannot figure out why it would suddenly stop working.
session_start();
define('MyConst', TRUE);
include "includes/server.php";
if (!(isset($_SESSION['name']) && $_SESSION['name'] != ''))
{
header("location:login.php");
}
$con = mysqli_connect($DB_HOST, $DB_USER, $DB_PASS, $DB_NAME) or die(mysqli_error($con));
$password1 = mysqli_real_escape_string($con, $_POST['newPassword']);
$password2 = mysqli_real_escape_string($con, $_POST['confirmPassword']);
$username = mysqli_real_escape_string($con, $_SESSION['name']);
$passwordhashed = password_hash("$password1", PASSWORD_DEFAULT);
if ($password1 <> $password2)
{
echo "your passwords do not match";
$referrer = $_SERVER['HTTP_REFERER'];
header ("Refresh: 2;URL='$referrer'");
}
else if (mysqli_query($con, "UPDATE accounts SET password='$passwordhashed' WHERE username='$username'"))
{
echo "You have successfully changed your password.";
$referrer = $_SERVER['HTTP_REFERER'];
header ("Refresh: 2;URL='$referrer'");
}
else
{
mysqli_error($con);
}
mysqli_close($con);
Expected to check that passwords match and echo "You have successfully changed your password" if the change was successful and then redirect.