r/PHPhelp • • May 19 '20

Login php not working, giving me error code wrongpswd1 althoug my pwd and user is correct thank you in advance for your help and time

<?php 
    if(isset($_POST['login-submit'])){
     require 'config.inc.php';
    $mailuid = $_POST['mailuid'];
    $password = $_POST['pwd'];
   if (empty($mailuid)|| empty($password)){
     header("Location:../index.php?error=emptyfields");
     exit();
    }
    else { 
        $sql = "SELECT * FROM user_account WHERE uidusers = ? OR emailusers = ?;";
        $stmt = mysqli_stmt_init($conn);
        if(!mysqli_stmt_prepare($stmt, $sql)){
            header("Location:../index.php?error=sqlerror");
     exit();
        }
        else {
           
            mysqli_stmt_bind_param($stmt, "ss", $mailuid, $mailuid);
            
            mysqli_stmt_execute($stmt);
            $result = mysqli_stmt_get_result($stmt);
            if($row = mysqli_fetch_assoc($result)){ 
                $pwsdcheck = password_verify($password, $row['pwdusers']);
                if($pwsdcheck == false){
                    header("Location:../index.php?error=wrongpwsd1");
                    exit();

                }
                else if ($pwsdcheck == true) {
                    session_start();
                    $_SESSION['userid'] = $row['id'];
                    $_SESSION['useruid'] = $row['uidusers'];
                    
                    header("Location:../main.php?login=success");
                    exit();
                }
                else {
                    header("Location:../index.php?error=wrongpwsd");
                    exit();
                }


            }
           else {
            header("Location:../index.php?error=nonexistinguser");
            exit();
           }
        }

    }
}
else {
    header("Location:../index.php");
    exit();
}
10 Upvotes

7 comments sorted by

2

u/bla4free May 19 '20

Verify the password form name is pwd. Do echo $_POST['pwd']; and verify PHP is getting the entered password.

Use password_hash() and re-encrypt the password for that user then try logging in again.

1

u/HKON_ May 19 '20

okay, think the hash might be the problem i discovered that the register script wont send hashed pwd into the db, after i added additional things which was gonna get posted from the form. Can you help me to get the hashed pwd to work? code for the registration file:

<?php require "config.inc.php";?>

<?php
if(isset($_POST\['signup-submit'\])) {
require 'config.inc.php';
$username = $_POST\['username'\];
$email=$_POST\['mail'\];
$password=$_POST\['pwduser'\];
$passwordrep=$_POST\['pwd-repeat'\];
$fname=$_POST\['fname'\];
$lname=$_POST\['lname'\];
$details=$_POST\['details'\];
$location=$_POST\['location'\];
$connections=$_POST\['connections'\];
if (empty($username) || empty($email) || empty($password) || empty($passwordrep)) {
header("Location:../signup.php?error=emptyfields&uid=". $username."&mail=". $email);
}
else if(!filter_var($email,FILTER_VALIDATE_EMAIL) && !preg_match("/\[a-zA-Z0-9\]\*$/",$username) ){
header("Location:signup.php?error=invalidmail&uid=");
exit();
}
elseif (!filter_var($email,FILTER_VALIDATE_EMAIL)){
header("Location:../signup.php?error=invalidmail&uid=".$username);
exit();
}
elseif (!preg_match("/\[a-zA-Z0-9\]\*$/",$username)){
header("Location:../signup.php?error=invalid&mail=".$email);
exit();
}
else if( $password !== $passwordrep){
header("Location:../signup.php?error=passwordcheck&uid=".$username."&mail=" .$email);
exit();
}
else {
$sql="SELECT uidusers FROM user_account WHERE uidusers=?";
$stmt = mysqli_stmt_init($conn);
if (!mysqli_stmt_prepare($stmt, $sql))
{
header("Location:../signup.php?error=sqlerror");
exit();
} else {
mysqli_stmt_bind_param($stmt, "s", $username);
mysqli_stmt_execute($stmt);
mysqli_stmt_store_result($stmt);
$resultcheck = mysqli_stmt_num_rows($stmt);
if ($resultcheck > 0){
header("Location:../signup.php?error=usertaken&mail=".$email);
exit();}
else {
$sql= "INSERT INTO user_account (uidusers, emailusers, pwdusers, first_name, last_name, details, connections, location_id) VALUES (\\"$username\\", \\"$email\\", \\"$password\\", \\"$fname\\", \\"$lname\\", \\"$details\\", \\"$connections\\", \\"$location\\")";
$stmt = mysqli_stmt_init($conn);
if (!mysqli_stmt_prepare($stmt, $sql)){
header("Location:../signup.php?error=sqlerror2");
exit();
}
else {
$hashedpwd = password_hash($password, PASSWORD_DEFAULT);
mysqli_stmt_bind_param($stmt, "ssssssss", $username, $email, $hashedpwd, $fname, $lname, $details, $connections, $location);
mysqli_stmt_execute($stmt);
header("Location:../index.php?signup=success");
exit();
}
}
}
}
mysqli_stmt_close($stmt);
mysqli_close($conn);
}
else {
header("Location:../signup.php");
exit();
}
?>

2

u/bla4free May 19 '20

Replace this line:

$sql= "INSERT INTO user_account (uidusers, emailusers, pwdusers, first_name, last_name, details, connections, location_id) VALUES ("$username", "$email", "$password", "$fname", "$lname", "$details", "$connections", "$location")";

With this line:

$sql= "INSERT INTO user_account (uidusers, emailusers, pwdusers, first_name, last_name, details, connections, location_id) VALUES (?, ?, ?, ?, ?, ?, ?, ?)";

When using prepared statements, you use ? to indicate where you watch to substitute data. In your original SQL statement, you had no ? so there was nothing to substitute. mysqli_stmt_bind_param() did not din any ? so it did not substitute any of the parameters with the parameters in your query. That is why your hashed password isn't being inserted.

1

u/HKON_ May 19 '20

thank you it worked!

1

u/bla4free May 19 '20

You're welcome!

1

u/lovesrayray2018 May 19 '20

I dont see code thats initializing $conn, but ur using it

$stmt = mysqli_stmt_init($conn);

1

u/HKON_ May 19 '20

followed a youtube tutorial which used it this way.