2

我想为我的登录系统使用脚本/ajax。到目前为止,我已经能够取回包含所有错误的数组,如果登录成功,请设置 $_SESSION['id']:

<script type="text/javascript">
$(document).ready(function() {
$("input[type=button]").click(function () {  
var username = $('input[id=username]').val(); // get the username 
var password = $('input[id=password]').val(); // and the password
    if (username != '' || password !='') { // if not empty 

     $.ajax({
            type: "POST",
            url: "loginUser.php",
            data : "username="+username+"&password="+password,
            dataType: "json",
            success: function (data) {
            var success = data['success'];
            if(success == 'false'){
            var error = data['message'];
            alert(error); // the array with all the errors
             }else{
       $('#mask , .login-popup').fadeOut(300 , function() {
       $('#mask').remove();  
    });// end fadeOut function()
setTimeout("location.href = 'home.php';",1500);
    }
  }
  });//end success function

  } else {
alert('Enter some text ! '); // just in case somebody to click witout writing anything :)
    }
    });//end click function
});//end ready function
  </script>

loginUser.php 基本上检查所有功能,然后像这样发回数据:

if (empty($_POST)===false){
$email = sanitize($_POST['username']);
$password = sanitize($_POST['password']);


if (empty($email) === true || empty ($password) === true){ 

    $errors[] = 'You need to enter a username and password';
} else if (mail_exists($email) === false){
    $errors[] = 'we can\'t find that username. have you registered?';
}else if (user_active($email) === false){
    $errors[] = 'you haven\'t activated your account!';
}else {
$login = login($email, $password);
if ($login === false){
    $errors[] = 'username/password combination is incorrect!';
}else {
//set user session 
$_SESSION['id'] = $login;

//redirect user to home
    echo json_encode(array("success"=>'true'
                ));
}

   }
echo json_encode(array("success"=>'false',
            "message"=>$errors,             
            ));
                          }

正如我所说,如果密码和用户名不正确,我会收到所有警报,如果密码和用户名正确,我会设置 $_SESSION 但弹出窗口仍然显示并且我没有重定向(但由于 SESSION 我可以访问放)。测试成功是 == true 还是 == false 是否正确???

* * *编辑:已修复,问题出在 php 文件中。看我的回答......

4

1 回答 1

2

固定的!问题出在 php 文件中的 if else 逻辑中:

JS:

<script type="text/javascript">
 $(document).ready(function() {
      $("input[type=button]").click(function () {
      var username = $('input[id=username]').val(); // get the content of what user typed ( in textarea ) 
      var password = $('input[id=password]').val(); // get the content of what user typed ( in textarea ) 
                            $.ajax({
                            type: "POST",
                            url: "loginUser.php",
                            data : "username="+username+"&password="+password,
                            dataType: "json",
                            success: function (data) {
                            var success = data['success'];
                            if(success == false){
        var error = data['message'];
                            alert(error); // just in case somebody to click on share witout writing anything :


                                }

                                if(success == true) {
   $('#mask , .login-popup').fadeOut(300 , function() {
   $('#mask').remove();  
                                });// end fadeOut function()
    setTimeout("location.href = 'home.php';",1000);                                 
                                                }
                                                    }

                        });//end ajax             
                 });//end click function
         });//end ready function

登录用户.php:

   <?php
    if (empty($_POST)===false){
$email = sanitize($_POST['username']);
$password = sanitize($_POST['password']);

if (empty($email) === true || empty ($password) === true){ 
            $result = false;
            $errors = 'You need to enter a username and password';
            echo json_encode(array("success"=>$result,
                                   "message"=>$errors,             
                                   ));
} else if (mail_exists($email) === false){
            $result = false;
            $errors= 'we can\'t find that username. have you registered?';
            echo json_encode(array("success"=>$result,
                                   "message"=>$errors,             
                             ));
}else if (user_active($email) === false){
            $result = false;
            $errors = 'you haven\'t activated your account!';
            echo json_encode(array("success"=>$result,
                            "message"=>$errors,             
                             ));
}else {
$login = login($email, $password);
if ($login === false){
            $result = false;
            $errors = 'username/password combination is incorrect!';
            echo json_encode(array("success"=>$result,
                            "message"=>$errors,             
                             ));
} else {
//set user session 
$_SESSION['user_id'] = $login;
$result = true;
    echo json_encode(array("success"=>$result
                               ));

}
}
}
?>
于 2012-10-10T16:07:32.633 回答