Php Programmers,
Why is the password_verify failing on this script ?
I do not think I got it wrong! Do you ?
You will see, it fails to log you in to your account with the correct Username/Email and Password.
Do check the script on your end in Xampp/Wamp.
Cheers.
It fails to log me in with the correct password. Column name: passwords. And not "password" or "Password" or "Passwords" as some might suspect I done a typo in column name when I have not.
Why is the password_verify failing on this script ?
I do not think I got it wrong! Do you ?
You will see, it fails to log you in to your account with the correct Username/Email and Password.
Do check the script on your end in Xampp/Wamp.
Cheers.
Code:
<?php /* ERROR HANDLING */ declare(strict_types=1); ini_set('display_errors', '1'); ini_set('display_startup_errors', '1'); error_reporting(E_ALL); mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT); include 'config.php'; // check if user is already logged in if (is_logged() === true) { //Redirect user to homepage page after 5 seconds. header("refresh:2;url=home.php"); exit; // } if ($_SERVER['REQUEST_METHOD'] == "POST") { if (isset($_POST["login_username_or_email"]) && isset($_POST["login_password"])) { $username_or_email = trim($_POST["login_username_or_email"]); // $password = $_POST["login_password"]; $hashed_password = password_hash($password, PASSWORD_DEFAULT); //Select Username or Email to check against Mysql DB if they are already registered or not. $stmt = mysqli_stmt_init($conn); if(strpos("$username_or_email", "@") === true) { $email = $username_or_email; $username = ""; $query = "SELECT ids, usernames, passwords, emails, accounts_activations_statuses FROM users WHERE emails = ?"; // i = integer; s = string; d = double; b = blob. mysqli_stmt_init($stmt); $stmt = mysqli_prepare($conn, $query); mysqli_stmt_bind_param($stmt, 's', $email); mysqli_stmt_execute($stmt); $result = mysqli_stmt_bind_result($stmt, $db_id, $db_username, $db_password, $db_email, $db_account_activation_status); // ... this line. But not both. } else { $username = $username_or_email; $email = ""; $query = "SELECT ids, usernames, passwords, emails, accounts_activations_statuses FROM users WHERE usernames = ?"; $stmt = mysqli_prepare($conn, $query); mysqli_stmt_bind_param($stmt, 's', $username); mysqli_stmt_execute($stmt); $result = mysqli_stmt_bind_result($stmt, $db_id, $db_username, $db_password, $db_email, $db_account_activation_status); // ... this line. But not both.# } //QUESTION: Which one of the following 3 to do and why that one over the other 2 ? $row = mysqli_stmt_fetch($stmt); //$row = mysqli_fetch_array($query, MYSQLI_ASSOC); //$row = mysqli_fetch_array($result, MYSQLI_ASSOC); mysqli_stmt_close($stmt); printf("%s (%s)\n",$row["usernames"],$row["passwords"]); echo "var_dump(row)";var_dump($row);?><br><?php //On test, this showing as: () bool(true); echo "var_dump(result)";var_dump($result)?><br><?php //On test, this showing as: () bool(true); if ($result == false) { echo "Incorrect User Credentials 1 - (query result == FALSE on LINE 79! )!<br>"; echo "Id from db: $db_id<br>"; echo "Email from db: $db_email<br>"; echo "Username from db: $db_username<br>"; echo "Hash from db: $db_password<br>"; echo "Account Activation Status from db: $db_account_activation_status<br>"; exit(); } elseif ($row['accounts_activations_statuses'] == '0') { { echo "You have not activated your account yet! Check your email for instructions on how to activate it. Check your spam folder if you don't find an email from us."; exit(); } } else { echo "Else got triggered on LINE 98! - (query result = TRUE)!<br>";//This ELSE is getting triggered on the test. That means $result = TRUE; echo "Id from db: $db_id<br>"; echo "Email from db: $db_email<br>"; echo "Username from db: $db_username<br>"; echo "Hash from db: $db_password<br>"; echo "Account Activation Status from db: $db_account_activation_status<br>"; } if (password_verify($password, (string)$row['passwords'])) { //If 'Remember Me' check box is checked then set the cookie. if(!empty($_POST["login_remember"])) // Either use this line .... //if (isset($_POST['login_remember']) && $_post['login_remember'] == "on") // ...or this line. But not both! { setcookie("login_username", $username, time()+ (10*365*24*60*60)); } else { //If Cookie is available then use it to auto log user into his/her account! if (isset($_COOKIE['login_username'])) { setcookie("login_username","",""); } } $_SESSION["user"] = $username; header("location:home.php?user=$username"); } else { echo "Incorrect User Credentials 2! (Else got triggered on LINE 124. Stating: 'password_verify = FALSE');<br>"; echo "Id from db: $db_id<br>"; echo "Email from db: $db_email<br>"; echo "Username from db: $db_username<br>"; echo "Hash from db: $db_password<br>"; echo "Account Activation Status from db: $db_account_activation_status<br>"; exit(); } } } ?> <!DOCTYPE html> <html> <head> <title><?php $site_name?> Member Login Page</title> <meta charset="utf-8"> </head> <body> <div class = "container"> <form method="post" action=""> <center><h3><?php $site_name ?> Member Login Form</h3></center> <div class="text-danger"> <div class="form-group"> <center><label>Username/Email:</label> <input type="text" placeholder="Enter Username" name="login_username_or_email" value="<?php if(isset($_COOKIE["login_username_or_email"])) echo $_COOKIE["login_username_or_email"]; ?>"</center> </div> <div class="form-group"> <center><label>Password:</label> <input type="password" placeholder="Enter password" name="login_password" value="<?php if(isset($_COOKIE["login_password"])) echo $_COOKIE["login_password"]; ?>"></center> </div> <div class="form-group"> <center><label>Remember Login Details:</label> <input type="checkbox" name="login_remember" /></center> </div> <div class="form-group"> <center><input type="submit" name="login_submit" value="Login" class="button button-success" /></center> </div> <div class="form-group"> <center><font color="red" size="3"><b>Forgot your password ?</b><br><a href="login_password_reset.php">Reset it here!</a></font></center> <center><font color="red" size="3"><b>Not registered ?</b><br><a href="register.php">Register here!</a></font></center> </form> </div> </body> </html>
Comment