codeigniter和jquery用户身份验证

时间:2012-08-25 15:24:36

标签: jquery codeigniter login

我正在尝试使用CodeIgniter和jQuery进行用户身份验证。我已设法执行以下操作:如果用户键入错误的用户名或密码,他将保留在登录页面上并使用jquery,他会收到输入错误用户名或密码的通知。

我的问题是成功,即如果他输入正确的用户名和密码,我设法通过简单的javascript重定向在登录页面上发送用户,但这不是我想要的。如果我使用简单的重定向与javascript,我无法填写用户会话数据,我需要在登录所有其他页面后跟踪用户。

我想如果可能,如果用户输入正确的用户名和密码,jquery什么都不做,并且只有在用户输入错误的用户名或密码时才激活功能,如果可能的话。

这是我的代码。首先是JS代码:

 formLogin.on('submit', function(e) {    
    e.preventDefault();
    $.ajax({
    type     : 'POST',
    url      : 'loginCheck',
    data     : formLogin.serialize(),
    dataType : 'json',
    success  : function(r) {


        if (r.status) {
            // this works, but I would like this part to do nothing
            // just to leave my PHP controller to redirect the user
            window.location = 'http://www.linkedin.com';

        } else {
            $('#errorMessageTop').fadeIn();

        }

    }
});

});

控制器代码是:

public function loginCheck()
    { 

       // set the validation rules
        $this->form_validation->set_rules('username', 'Username', 'required|trim|encode_php_tags');
        $this->form_validation->set_rules('password', 'Password', 'required|trim|encode_php_tags');
        $this->form_validation->set_error_delimiters('<br /><p class=jsdiserr>', '</p><br />');
        if ($this->form_validation->run() != FALSE) 
        {

            $ids=array();
            $ids[0]=$this->db->where('username', $this->input->post('username'));
            $ids[1] = $this->db->where('password', md5($this->input->post('password')));
            $query = $this->backOfficeUsersModel->get();

            if($query)
                {
                    $data = array(
                    'username'       => $this->input->post('username'),
                    'isUserLoggedIn' => true
                    ); 
                $isAuthenticated = true;
                if ($isAuthenticated) {
                 $return['status']  = true;
                $return['message'] = 'You have successfully been logged in!'; 
                exit(json_encode($return)); 
                 $this->session->set_userdata($data);
                $data['title'] = "Welcome to dashboard!";
                $data['main_content'] = 'dashboard';
                $this->load->vars($data);
                $this->load->view('backOffice/template');


                }

        } else {   

                $return = array(
                'status'  => false,
                'message' => 'Wrong Username or Password'
                );
                 exit(json_encode($return));

        }
        } else {

             $errorMessage = "Second Message Wrong username or pwd...!";
             $errorMessage = json_encode($errorMessage);

        }



    } // end of function loginCheck 

任何帮助都将深受赞赏。

的问候,卓然

===============

来自控制器的新代码:

public function loginCheck()
    { 

       // set the validation rules
        $this->form_validation->set_rules('username', 'Username', 'required|trim|encode_php_tags');
        $this->form_validation->set_rules('password', 'Password', 'required|trim|encode_php_tags');
        $this->form_validation->set_error_delimiters('<br /><p class=jsdiserr>', '</p><br />');
        if ($this->form_validation->run() != FALSE) 
        {

            $ids=array();
            $ids[0]=$this->db->where('username', $this->input->post('username'));
            $ids[1] = $this->db->where('password', md5($this->input->post('password')));
            $query = $this->backOfficeUsersModel->get();

            if($query)
                {
                    $data = array(
                    'username'       => $this->input->post('username'),
                    'isUserLoggedIn' => true
                    );


                $this->session->set_userdata($data);
                echo json_encode(array("success" => true));
                $data['title'] = "Welcome to dashboard!";
                $data['main_content'] = 'dashboard';
                $this->load->vars($data);
                $this->load->view('backOffice/template');


        } else {   

                echo json_encode(array("success" => false, "error" => "Wrong credentials"));

        }
        } else {

             $errorMessage = "Second Message Wrong username or pwd...!";
             $errorMessage = json_encode($errorMessage);

        }



    } // end of function loginCheck 

和js代码:

  $("#formLogin").submit(function(e){
        e.preventDefault();
        var username = $(this).find("#username").val();
        var password = $(this).find("#password").val();
        var obj = {username: username, password: password};
        var url = $(this).attr("action");
        $.post(url, obj, function(r){
            if(r.success) window.location.replace('http://www.linkedin.com');
            else  $('#errorMessageTop').fadeIn();
        }, 'json');
    })

1 个答案:

答案 0 :(得分:1)

为什么要调用exit()?...这会终止脚本并阻止设置会话。此外,您将$isAuthenticated设置为true并立即检查是否true。当然会是。

这是我如何处理它的精简版...在我的控制器中:

function take_creds(){

        $p = $this->input->post();
        if($p){
            $u = new User();
            $u->where("username", $p['username']);
            $u->where("password", sha1($p['password']));
            $u->get();
            if($u->exists()){
                $u->loggedin = 1;
                $u->save();

                $data = array(
                    'username' => $u->username,
                    'loggedin' => true
                );

                $this->session->set_userdata($data);
                echo json_encode(array("success" => true));
            }
            else echo json_encode(array("success" => false, "error" => "Wrong credentials"));   
        }
    }

和相应的jQuery:

$("#login_form").submit(function(e){
            e.preventDefault();
            var username = $(this).find("#login_username").val();
            var password = $(this).find("#login_password").val();
            var obj = {username: username, password: password};
            var url = $(this).attr("action");
            $.post(url, obj, function(r){
                if(r.success) window.location.replace('home');
                else console.log("Write this error to the DOM: " + r.error);
            }, 'json');
        })