Ajax php帖子没有验证

时间:2016-09-22 20:08:43

标签: javascript php jquery ajax

我有一个示例脚本,用于验证我的用户是否可以访问该页面。我的问题是,当我发布值时,js文件确实反映数据已被序列化但是当它被发布到php文件以检查数据库记录是否存在时,用户仍然可以访问该页面,无论登录详情是否正确或者错了。出于某种原因,它似乎没有采用我的`$ _POST ['pass']和我的$ _POST ['user_email']值。但是,如果我手动输入php文件中的用户电子邮件和密码来替换变量,它将起作用。

HTML表单

<form class="login" id="login-form" name="login-form" method="post">
    <p class="title">LOGIN</p>
    <input type="text" placeholder="Email" id="user_email" name="user_email" autofocus/>
    <i class="fa fa-user"></i>
    <input type="password" placeholder="Password" id="pass" name="pass" />
    <i class="fa fa-key"></i>
     <button>
      <i class="spinner" style="outline:none;"></i>
      <span class="state">Log in</span>
    </button>
  </form>

我的js文件发布值。我添加了console.log,只是为了测试脚本所采用的值

$('document').ready(function()
{ 
    var working = false;
    $('.login').on('submit', function(e) {
        e.preventDefault();
        if(working)return
        working = true;
        var $this = $(this),
        $state = $this.find('button > .state');
        $this.addClass('loading');
        $state.html('Authenticating');

        var data = $("#login-form").serialize();
        console.log(data);

        $.ajax({

            type : 'POST',
            url  : 'login_process.php',
            data : data,
            success :  function(response) {                     
                    console.log(response);
                    if(response=="ok"){
                        setTimeout(function() {
                            $this.addClass('ok');
                            $state.html('Welcome');

                            setTimeout(function() {
                                $state.html('Log in');
                                $this.removeClass('ok loading');
                                working = false;
                            }, 4000);

                            setTimeout(function() {
                                window.location.href = "/Home.aspx";
                            }, 4000);  
                        }, 3000);           
                        //$("#btn-login").html('<img src="btn-ajax-loader.gif" /> &nbsp; Signing In ...');
                        //setTimeout(' window.location.href = "home.php"; ',4000);
                    } else {
                        console.log('ERROR IN LOGINING IN');    
                    }
              }
            });
            return false;
    });
});

PHP文件'login_process'

<?php
    session_start();
    require_once 'dbconfig.php';
    if(isset($_POST['pass']))
    {
        $user_email = urldecode(trim($_POST['user_email']));
        $user_password =trim($_POST['pass']);
        //$password = md5($user_password);
        $password = $user_password;
        try {   

            $stmt = $db_con->prepare("SELECT * FROM tbl_users WHERE user_email=:email");
            $stmt->execute(array(":email"=>$user_email));
            $row = $stmt->fetch(PDO::FETCH_ASSOC);
            $count = $stmt->rowCount();

            if($row['user_password']==$password){
                echo "ok"; // log in
                $_SESSION['user_session'] = $row['user_id'];
            }
            else{
                echo "email or password does not exist."; // wrong details 
            }
        }
        catch(PDOException $e){
            echo $e->getMessage();
        }
}
?>

1 个答案:

答案 0 :(得分:-1)

你正在使用dataType缺少dataType make:'json'就在数据之后,你可以通过json_encode()返回结果json_encode()ti debug result