2017-10-18 144 views
0

我目前正在使用AJAX登录系统,但它不会发送任何回应。它只发送一个响应,当我exit("error here")脚本,但我想返回一个JSON响应。PHP - AJAX登录不工作

我的形式:

<div id="account" class="modal" role="dialog"> 
    <div class="modal-dialog"> 
     <form method="post" id="login-form"> 
      <div class="modal-content"> 
       <div class="modal-header"> 
        <h4 class="modal-title">Sign in</h4> 
       </div> 
       <div class="modal-body"> 
         <p> 
          Email address:<br /> 
          <input type="email" class="form-control" placeholder="Email address" name="user_email" id="user_email" /> 
         </p> 
         <p> 
          Password:<br /> 
          <input type="password" class="form-control" placeholder="Password" name="password" id="password" /> 
         </p> 
        </form> 
       </div> 
       <div class="modal-footer"> 
        <button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button> 
        <button type="submit" class="btn btn-default" name="btn-login" id="btn-login">LOGIN</button> 
       </div> 
      </div> 
     </form> 
    </div> 
</div> 

我的javascript:

$("#btn-login").click(function(e) { 
     alert('submit'); 
     var data = $("#login-form").serialize(); 
     alert(data); 
     $.ajax({ 
      url: 'ajax.php', 
      type: 'POST', 
      data: data, 
      success: function(response) { 
       var data = response.responseText; 
       var resp = $.parseJSON(data); 

       if (resp.error) { 
        alert('error: ' + response); 
       } else { 
        alert('success: ' + response); 
       } 
      } 
     }); 
     event.preventDefault(); 
    }); 

我的PHP:

if(isset($_POST['user_email']) && is_ajax()) { 
    $engine->expireLoginAttempts(); 
    $engine->expireLoginBan(); 

    if(!$engine->isLoginBanned()) { 
     $email = strip_tags($_POST['user_email']); 
     $password = strip_tags($_POST['password']); 

     if($engine->doLogin($email,$password)) { 
      $engine->expireBan(); 

      if($maintenanc['value'] == '1' && !$engine->isStaff()) { 
       echo json_encode(array(
        'success' => '0', 
        'message' => 'You can\'t login using a regular account because the site is currently under maintenance.' 
       )); 
      } elseif($engine->isBanned() && !$engine->isOwner()) { 
       // Account banned; notify and give a timestamp + reason 
       $stmt = $engine->runQuery("SELECT timestamp, expiration, reason FROM banned WHERE user_id=:user_id"); 
       $stmt->execute(array(':user_id'=>$_SESSION['user_id'])); 
       $userRow = $stmt->fetch(PDO::FETCH_ASSOC); 
       $blockdate1 = strtotime('+' . $userRow['expiration'], strtotime($userRow['timestamp'])); 
       $blockdate2 = date('d-m-Y H:i:s', $blockdate1); 

       echo json_encode(array(
        'success' => '0', 
        'message' => 'This account is suspended until ' . $blockdate2 . ' because of the following reason: ' . $userRow['reason'] . '.' 
       )); 
      } else { 
       echo json_encode(array(
        'success' => '1', 
        'message' => $_SESSION['user_name']; 
       )); 
      } 
     } else { 
      // Create login attempt if the account exists and the ip is different from the register ip 
      $stmt = $engine->runQuery("SELECT user_email, user_id, user_ip FROM users WHERE user_email=:email"); 
      $stmt->execute(array(':email'=>$email)); 
      $userRow = $stmt->fetch(PDO::FETCH_ASSOC); 
      if($stmt->rowCount() == 1 && $userRow['user_ip'] !== $_SERVER["REMOTE_ADDR"]) { 
       $engine->createLoginAttempt($userRow['user_id'], $_SERVER["REMOTE_ADDR"]); 
      } 

      // Bruteforce attack suspected, bans the ip after 10 attempts from an unknown ip 
      $attemptban = $engine->runQuery("SELECT * FROM loginattempt WHERE ip=:ip"); 
      $attemptban->execute(array(':ip'=>$_SERVER["REMOTE_ADDR"])); 
      if($attemptban->rowCount() > 4) { 
       if($engine->loginban()) { 
        $engine->deleteLoginAttempts($_SERVER["REMOTE_ADDR"]); 
       } 
      } 

      echo json_encode(array(
       'success' => '0', 
       'message' => 'Emailaddress and/or password invalid.' 
      )); 
     } 
    } 
} 

有谁知道我做错了吗?我试过exit ing json_encode,在每个json_encode之后放exit(),但没有一个看起来有效。任何帮助都非常感谢!

+0

你并不需要分析自己的JSON响应; jQuery可以为你做到这一点。只需设置'dataType:'json'' – Phil

+0

1)为你的'$ .ajax'请求添加一个错误处理程序。签名是'error:function(jqXhr,textStatus,errorThrown)'。 2)检查您的浏览器的*网络*控制台的POST请求。答案是什么样的? – Phil

+0

我也会使用'$(“#login-form”)。submit'而不是'$(“#btn-login”)。click'来捕捉事件。可以通过在文本字段中敲击“Enter”来提交表单,只有监听按钮单击才会错过 – Phil

回答

0

尝试添加一些东西给别人看看发生了什么。

if(!$engine->isLoginBanned()){ 
 
    ... 
 
} else { 
 
    // Try add something here and see what's happen, eg. 
 
    echo json_encode(array(
 
     'success' => '0', 
 
     'message' => 'Sorry, isLoginBanned'; 
 
    )); 
 
}