使用ajax codeigniter检查用户名和密码是否匹配

时间:2017-06-11 07:02:52

标签: php jquery ajax codeigniter

我目前正在做一个项目,在我的项目中有一个登录模式。到目前为止我使用ajax完成的事情是:1)检查两个字段的用户名和密码是否有值; 2)用户名字段只有一个值(密码字段为空); 3)检查帐户是否已经过验证

注意:我刚尝试if($ data ['validateLogin']> 0)如果它有一个值,是的它正在工作但是当我输入正确的用户名和密码时它仍然给我一个错误“用户名不正确或密码“

问题:如何检查用户名和密码是否正确?我的病情错了吗? if($ data ['validateLogin']> 0)?

登录模式视图

<!-- Login -->
            <div id="myLogin" class="modal fade" role="dialog">
                <div class="modal-dialog">
                    <!-- Modal content-->
                    <div class="modal-content">
                        <div class="modal-header">
                            <button type="button" class="close" data-dismiss="modal">&times;</button>
                            <h4 class="modal-title">Login</h4>
                        </div>
                        <div class="modal-body">
                            <form id="login" encrypt="multipart/form-data">
                                <div class="form-group">
                                    <label> Username: </label>
                                    <input type="text" class="form-control" name="username" id="username">
                                </div>
                                <div class="form-group">
                                    <label> Password: </label>
                                    <input type="password" class="form-control" name="password" id="password">
                                </div>
                        </div>
                        <div class="modal-footer">
                            <button type="submit" class="btn btn-primary"> Login </button>
                            </form>
                            <a data-toggle="modal" href="#mySignup" data-dismiss="modal">Sign up</a>
                        </div>
                    </div>

                </div>
            </div>

我的登录模式的AJAX

$("#login").on('submit',function(event){

          $.ajax({
              url: "http://localhost/itsq/User/validate_user",
              type: "POST",
              data: $(this).serialize(),
              success: function(data) {
                  var result = JSON.parse(data);
                  //alert(data);
                  if(result===1)
                          {
                                swal({
                                  type: 'success',
                                  html: 'Update Successful!',
                                  timer: 2000,
                                  })
                                setTimeout(function() {

                                  document.location.href=base_url + "User/";

                                }, 2000);
                            // document.location.href="http://localhost/ecom/Administrator/view_staff_Account";

                          }
                          else
                          {

                                swal({
                                  type: 'error',
                                  html: result,
                                  timer: 2000,
                                  })

                            console.clear();
                          }
                  // $.LoadingOverlay("hide");
              },
              error: function (xhr, ajaxOptions, thrownError) {
           console.log(xhr.status);
           console.log(xhr.responseText);
           console.log(thrownError);
       }
          })
          event.preventDefault();
    });

控制器

public function validate_user()
    {
        $this->form_validation->set_error_delimiters('<div class="alert alert-danger" role="alert" style="padding:2px">', '</div>');
        $this->form_validation->set_rules('username', 'Username', 'required|trim');
        $this->form_validation->set_rules('password', 'Password', 'required|trim');
        if ($this->form_validation->run() == FALSE)
        { //if validation is false go to itemList
            echo json_encode(validation_errors());

        }
        else
        {

            $username = $_POST['username'];
            $password = $_POST['password'];

            $data['validateLogin'] = $this->CrudModel->validate_user($username,md5($password));

            if($data['validateLogin'] > 0) // If there's no match then do this
            {

                echo json_encode("Incorrect username or password");
            }
            else // Get all the information for that account
            {
                foreach($data['validateLogin'] as $vl) // Save it to one variable
                {
                     $user_id = $vl->id;
                     $status = $vl->status;
                }

                if($status != "Verified") // Is not verified
                {
                    echo json_encode("Your account is not verified");


                }
                else
                {
                    $this->session->set_userdata(array('user' => true, 'first_name' => $first_name, 'middle_name' => $middle_name, 'last_name' => $last_name,'gender' => $gender,'age' => $age, 'email' => $email,'username' => $username,'address' => $address, 'credit_card' => $credit_card, 'bank_name' => $bank_name));
                    redirect('administrator/index',$data);
                    echo json_encode(1); 
                }
            }


            // $this->CrudModel->insert('users', $customer);
            // echo json_encode(1); 
        }


    }

模型

// public function validate_user($table,$username_column,$email_column,$password_column,$username, $password)
public function validate_user($table,$username, $password)
{
    $this->db->select('username,email,password');
    $this->db->where("(email = $username OR username = $username) AND password = $password");
    // $query = $this->db->get($table);
    $query = $this->db->get();
    echo $this->db->last_query();
    return $query->result();
    // $query = $this->db->get_where($table,array($username_column => $username,$password_column => $password));
 //    return $query->result();
}

2 个答案:

答案 0 :(得分:2)

你需要改变你的状况

if(count($data['validateLogin']) == 0) // If there's no match then do this

答案 1 :(得分:1)

如果validate_user有结果,则会返回true。如果没有任何结果,则会返回false

您可以通过执行以下操作来检查validate_user结果的有效性。将检查&#34;如果此值为false,我们将回显错误。否则,我们将继续登录过程&#34;。

// if NOT true
if(!$data['validateLogin']){
  echo json_encode("Incorrect username or password");
}
else {
...
}