I got the following code and nothing is getting inserted into my database and I can't figure out why.. is the password_needs_rehash()
function at it's right spot?
I got a test where I echo out the new generated with a Javascript alert and it shows a new hash but the new hash is not getting inserted into the database. There must be a error in my logic but I can't find it.
<?php
session_start();
include 'mysqli.php';
$userID = strtolower($_POST['userID']);
$passwordFromForm = $_POST['password'];
$_SESSION['userID'] = $userID;
//Select the password from the requested userID and put into variable
$sql = "SELECT password FROM userdata WHERE userID = '$userID'";
$result = mysqli_query($conn, $sql);
$row = mysqli_fetch_assoc($result);
$passwordFromDatabase = $row['password'];
$verified = password_verify($passwordFromForm, $passwordFromDatabase);
if($verified == 0){
echo '<meta http-equiv="refresh" content="0; URL=index.php?triedLogin" />';
}else{
if(password_needs_rehash($passwordFromDatabase, PASSWORD_DEFAULT, ['cost' => 14])){
$passwordFromDatabase = password_hash($passwordFromDatabase, PASSWORD_DEFAULT, ['cost' => 14]);
echo "<script>alert('".$passwordFromDatabase."');</script>";
$sql = "INSERT INTO userdata ('password') VALUES ('$passwordFromDatabase')";
//NOW LOGIN WITH NEW PASSWORD
$sql = "SELECT * FROM userdata WHERE userID = '$userID' AND password = '$passwordFromDatabase'";
$result = mysqli_query($conn, $sql);
if(!$row=mysqli_fetch_assoc($result)){
echo '<meta http-equiv="refresh" content="0; URL=index.php?triedLogin" />';
} else {
$_SESSION['ID'] = $row['userID'];
unset($_SESSION['userID']);
echo '<meta http-equiv="refresh" content="0; URL=app/index.php" />';
}
}else{
$sql = "SELECT * FROM userdata WHERE userID = '$userID' AND password = '$passwordFromDatabase'";
$result = mysqli_query($conn, $sql);
if(!$row=mysqli_fetch_assoc($result)){
echo '<meta http-equiv="refresh" content="0; URL=index.php?triedLogin" />';
} else {
$_SESSION['ID'] = $row['userID'];
unset($_SESSION['userID']);
echo '<meta http-equiv="refresh" content="0; URL=app/index.php" />';
}
}
}
?>