首页 > 解决方案 > 获取不正确的密码

问题描述

我不知道为什么我的密码不正确,我的注册页面工作正常。每次我尝试登录时,它都会在我的 url 中显示 login=incorrect password。我试图在网上找出所有可能的问题,但没有任何帮助我。

    <?php 
    session_start();

      if(isset($_POST['submit'])){

    include_once 'dbt.inc.php';

    $username = mysqli_real_escape_string($conn, $_POST['username']);
    $password = mysqli_real_escape_string($conn, $_POST['password']);

    //error handlers
    if(empty($username) || empty($password)){
        header("Location: ../main_login.php?login=empty");
        exit();
    }
    else{
        $sql = "SELECT * FROM users WHERE user_username = '$username'";
        $run = mysqli_query($conn, $sql);
        $result = mysqli_num_rows($run);

        if ($result < 1) {
            header("Location: ../main_login.php?login=error");
            exit(); 
        }
        else{
            if ($row = mysqli_fetch_assoc($run)) {
                $hashedpasswordcheck = password_verify($password, $row['user_password']);
                if ($hashedpasswordcheck == false) {
                    header("Location: ../main_login.php?login=incorrect password");
                    exit();
                }
                elseif($hashedpasswordcheck == true){
                    //log in user
                    $_SESSION['user_id'] = $row['user_id'];
                    $_SESSION['user_first'] = $row['user_first'];
                    $_SESSION['user_last'] = $row['user_last'];
                    $_SESSION['user_email'] = $row['user_email'];
                    $_SESSION['user_username'] = $row['user_username'];
                    $_SESSION['user_password'] = $row['user_password'];
                    header("Location: ../main_login.php?login=success");
                    exit();

                }
            }
        }

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



 ?>

这是我的注册码它工作得很好,我没有在这个代码中看到任何错误,所以如果你看到任何错误,请帮助我

 <?php 
if(isset ($_POST['submit'])){
    include_once 'dbt.inc.php';

    $first = mysqli_real_escape_string($conn, $_POST['first']);
    $last = mysqli_real_escape_string($conn, $_POST['last']);
    $email = mysqli_real_escape_string($conn, $_POST['email']);
    $username = mysqli_real_escape_string($conn, $_POST['username']);
    $password = mysqli_real_escape_string($conn, $_POST['password']);

        //Error Handlers
    if(empty($first) || empty($last) || empty($email) || empty($username) || empty($password)){

            header("Location: ../main_signup.php?signup=empty");
            exit();
    }
    else{
        //check if input characters are valid
        if(!preg_match("/^[a-zA-Z]*$/", $first) || !preg_match("/^[a-zA-Z]*$/", $last)){
            header("Location: ../main_signup.php?signup=invalid first and last name");
            exit();
        }
        else{
            //check if email is valid
            if(!filter_var($email, FILTER_VALIDATE_EMAIL)){
                    header("Location: ../main_signup.php?signup=invalid_email");
                    exit();
            }
            else{
                $sql = "SELECT * FROM users WHERE user_username='$username'"; 
                $result = mysqli_query($conn, $sql);
                $resultCheck = mysqli_num_rows($result);

                if($resultCheck > 0){
                        header("Location: ../main_signup.php?signup=username already taken");
                        exit();

                }else{
                            //hashing the password
                            $hashed_password = password_hash($password, PASSWORD_DEFAULT);

                            //insert the user into database
                            $sql = "INSERT INTO users (user_first, user_last, user_email, user_username, user_password) VALUES('$first','$last','$email','$username','$hashed_password ')";
                            $run = mysqli_query($conn, $sql);
                            header("Location: ../main_signup.php?signup=success");
                            exit();
                        }
            }
        }
    }
}
else{
    header("Location: ../main_signup.php");
}
 ?>

标签: phpsql

解决方案


您在注册代码中的 hashed_pa​​ssword 变量后面有一个空格。

'$username','$hashed_password ')";

推荐阅读