显示错误的密码或用户名,即使它是正确的密码或用户名

时间:2018-06-11 08:09:14

标签: php jquery html ajax mysqli

我正在使用AJAX创建登录表单,我试图让它工作4天但无法这样做  在这个我有几个问题,即,如果我输入有效的用户名&密码也然后它没有登录到页面,我不知道我哪里出错了请  任何人都可以找到我出错的地方,它会非常有帮助。

的login.php

<?php 
    session_start(); 
    $mysqli  = mysqli_connect("localhost","root","","ajax1");
?>
<!DOCTYPE HTML>  
<html>

    <head>
        <title> login script with ajax</title>
        <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
    </head>

    <body style="background-color:#b3ffff">

        <div style="padding-left:500px ; padding-top:200px">

            Username:   <input id="username" type="text" name="username" placeholder="username"><br><br>
            Password:   <input id="password" type="password" name="password" placeholder="Password"><br><br>
                        <input id="submit" name="submit" type="button" value="Log In">
                        <p style="color:black">Havent Registered? <a href="index.php">Register</a>.</p><br><br>
                        <div id="display" style="color:red"></div>

            <script>
                $(document).ready(function(){
                    $("#submit").click(function(){
                        var password = $("#username").val();
                        var password = $("#password").val();

                        var datastring = 'username=' + username + '&password=' + password;

                        if(username=='' || password==''){
                            $("#display").html("Please Enter All The Fields");
                        }
                        else{
                            $.ajax({
                            type: "POST",
                            url: "success.php",
                            data: datastring,
                            cache: false,
                            success: function(result){
                                    $("#display").html(result);
                                    window.location = "welcome.php";
                                }
                            });
                        }
                        return false;
                    });
                });
            </script>
        </div>

    </body>
</html>

success.php

<?php

        $mysqli  = mysqli_connect("localhost","root","","ajax1");
        session_start();

        if (isset($_SESSION['id'])){
            header('location:welcome.php');
        }

        $myusername = mysqli_real_escape_string($mysqli,$_POST['username']);
        $mypassword = mysqli_real_escape_string($mysqli,$_POST['password']); 

        $sql = "SELECT * FROM users WHERE username = '$myusername' AND password='$hashed_password'";
        $result = mysqli_query($mysqli,$sql);
        $row = mysqli_fetch_array($result);
        $hashed_password=$row['password'];

        if(password_verify($mypassword, $hashed_password)) {
            $_SESSION['login_user'] = $myusername;
            //$_SESSION['id']=$row['userid'];
            echo'Successfully Registered';
        exit();
        }    
        else 
        {
        echo'Invalid username or password';
        }
?>

的welcome.php

<?php
    session_start();
    if (!isset($_SESSION['id'])) {
        header('location:login.php');
    }
?>

<!DOCTYPE html>
<html>
    <body>

        <div style="Padding-left:200px; padding-top:100px">
            <?php
                $mysqli  = mysqli_connect("localhost","root","","ajax1");
                $query=mysqli_query($mysqli,"select * from `users` where userid='".$_SESSION['id']."'");
                $row=mysqli_fetch_array($query);
                echo 'Welcome - '.$row['username'];
            ?>

            <!--br><br>
                <a href="logout.php">Logout</a>
            <br><br-->

        </div>

    </body>
</html>

3 个答案:

答案 0 :(得分:2)

删除AND password='$hashed_password'

来自

$sql = "SELECT * FROM users WHERE username = '$myusername' AND password='$hashed_password'";

只检查用户名是否存在

使用

$sql = "SELECT * FROM users WHERE username = '$myusername'";

然后使用password_verify()

检查密码

改变你的

password_verify(){
     echo 'success';
}else{
  echo 'error';
}

和ajax

success:function(data){
   if(data.trim() == 'success'){
       window.location.href='success.php'
    }else if(data.trim()== 'error'){
       ///use some javascript to display the error message
    }
}

答案 1 :(得分:1)

在login.php的提交功能中,您设置了错误的变量。将它们改为:

var username = $("#username").val();
var password = $("#password").val();

在你这样做之后,去做Sugumar在答案中建议的内容。

答案 2 :(得分:1)

您已经定义了两次密码而不是用户名。

var password = $("#username").val();
var password = $("#password").val();

更正此问题并再次检查您的代码。