jQueryValidation远程方法,用于检查密码是否与数据库记录匹配

时间:2017-04-25 09:46:01

标签: php mysql validation

假设我们有一个注册用户,用户名=“albert”和密码=“albert”。显然,密码与
存储 password_hash($_POST['password'],PASSWORD_BCRYPT)

要检查用户名/密码对是否与数据库中的记录匹配,服务器端验证已完成并且运行良好。

在线验证密码时,我发现jQueryValidation插件的远程方法有困难。

通过比较用户名和密码验证,我认为错误来自远程方法发送的密码值之间的差异(2个参数) 和我的sql查询的公式(1参数)。

任何人都可以帮我看清楚吗?

<form id="form_membre_cnx" action="" method="POST">
    <div>
        <input name="username" id="username_input_cnx" type="text" autofocus>
        <label for="username_input_cnx" class="field__label">Username :</label>
    </div>
    <div class="field__message"></div>
    <div>
        <input name="password" id="password_input_cnx" type="password">
        <label for="password_input_cnx" class="field__label">Password :</label>
    </div>
    <div class="field__message"></div>                          
    <button type="submit" name="submit_cnx" id="submit_cnx" class="button--full-width btn btn-primary">Log in</button>
</form>

check_matching_username.php:

<?php

    require_once('inc/db.php');
    $req = $pdo->prepare('SELECT id FROM users WHERE username= ?');

    $req->execute([$_POST['username']]);
    $user = $req->fetch();
    if($user){
        echo 'true';
    }else {
        echo 'false'; 
    }   

?> 

check_matching_password.php:

<?php

    require_once('inc/db.php');

    //$req=$pdo->prepare('SELECT * FROM users WHERE (username = :username OR email = :username) AND confirmed_at IS NOT NULL ');
    //$req->execute( ['username'=> $_POST['username']]);

    $req = $pdo->prepare('SELECT id FROM users WHERE username=? AND password=?'); 
    $req->execute([ $_POST['username'],$_POST['password'] ]);

    $user=$req->fetch();

    if( password_verify($_POST['password'],$user['password'] ) ){
        echo 'true';
    }else{
        echo 'false';
    }

?> 

validation.js:

$("#form_membre_cnx").validate({
    errorElement: "span"
    ,errorPlacement: function(error, element) {
        error.appendTo( element.parent().next("div.field__message") );
    }       
    ,rules:{
        username: {
            required: true
            ,remote: {
                url: "check_matching_username.php"
                ,type: "post"
            }                    
        }               
        ,password: {
            required: true
            ,remote: {
                url: "check_matching_password.php"
                ,type: "post"                    
                ,data: {
                    username:function(){                            
                        return $("#username_input_cnx").val();                            
                    }                       
                } 
            }
        }
    }
    ,messages: {
        username:{
            required: 'This field is required'
            ,remote: "Unknown username"
        }               
        ,password:{
            required: 'This field is required'
            ,remote: "The password is incorrect"
        }               
    }
}); 

firebug-&gt;网络 - &gt; XHR: enter image description here

1 个答案:

答案 0 :(得分:0)

最后,我找到了解决方案。正如我猜测的那样,问题来自于我的SQL查询。

这是正确的(验证.js是正确的,所以变更):

<?php
    require_once('inc/db.php');

    $req=$pdo->prepare('SELECT * FROM users WHERE username = :username ');
    $req->execute( ['username'=> $_POST['username']]);
    $user=$req->fetch();
    if(password_verify($_POST['password'],$user->password) ) {
        echo 'true'; 
    }else{
        echo 'false';
    }

?>