簡體   English   中英

Ajax php帖子沒有驗證

[英]Ajax php post not authenticating

我有一個示例腳本,用於驗證我的用戶是否可以訪問該頁面。 我的問題是,當我發布值時,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();
        }
}
?>

你正在使用dataType缺少dataType make:'json'就在數據之后,你可以通過json_encode()返回結果json調試結果

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM