Redirect in codeigniter after login

2019-08-02 19:24发布

问题:

Trying to do a redirect after a successful login. The login info is sent using ajax to the controller.

My controller code as below

public function login_controller_function()
{
    $this->load->model('login_model');
    if ($this->input->is_ajax_request())
    {
        $user_name=$this->input->post('username');
        $user_password = $this->input->post('password');
        $this->load->helper('url');
        $result = $this->login_model->verify_user($user_name,$user_password);
        //  echo 'user_logged_in';
        if(strcmp($result,'user_logged_in')==0)
        {
            redirect('welcome');

        }
    }
}

But it is not working at all. Anyone knows whats wrong?

Hi my html as requested. i am using twitter bootstrap as well

<li class="divider-vertical"></li>
<li><a href="#" id="login_btn">Login</a></li>
<li><a href="<?php echo base_url('register'); ?>">Register</a></li>

for the buttons so when i click the login button, a modal window will appear and ask for login info.

so when i click on the login button, my js code will send an ajax request

my js code as below

    /*Attempt register user jquery ajax*/
    $('#login').click(function(){ 

        var user_name = $('#loginHere').find('#user_name').val();

        var user_password = $('#loginHere').find('#login_pwd').val();

        if(user_name==""||user_password=="")
            return;

        var login_data = { username:user_name, password:user_password};


                $.ajax({
        type: "POST",
        dataType: "json",
        async: false,
        url:"login_register/login_controller_function",
        data: login_data,
        success: function(data) {
            if(data.login)
            {
                alert(data.redirect);
                window.location.replace(data.redirect);

            }
            else if(!data.login)
            {
                alert('data login not true');
            }
        },
        error:function(data){
            alert('ajax error');
        }
    });
    });
});

回答1:

As you are calling the controller method using ajax, you need to send back some variable or value (to indicate successful or unsuccessful login) back to the javascript which made the ajax call to handle the redirect.Or in other words the javascript should handle the redirect since you are not making the call to the controller by directly posting to the controller method, but rather via ajax.

What the ajax script will output as response to the ajax call after login and then redirecting to the welcome controller is the entire HTML of the page to which the controller method will redirect, which is not the correct way to handle the ajax call.

So I would suggest , you should rather set some session values in the controller after the login is successful and send back some value to the ajax method to indicate iof the login was successful or failure.

You can then redirect with javascript, and you should have the sessions set when you get redirected to the secure page by javascript.

Controller:


public function login_controller_function(){
$this->load->model('login_model');
if ($this->input->is_ajax_request())
{
    $user_name=$this->input->post('username');
    $user_password = $this->input->post('password');
    $this->load->helper('url');
    $result = $this->login_model->verify_user($user_name,$user_password);
    //  echo 'user_logged_in';
    if(strcmp($result,'user_logged_in')==0)
    {
     //set the session variables here so that I can access the secure area 

     //send back response to the ajax method  
     print 'success';

    }else{
        //send back response to the ajax method
        print 'failure';
    }
}

}

ajax script:


$.ajax({
       type: "POST",
       url: "<?php echo base_url();?>index.php/login/login_controller_function",
       data: "userName="+$('#userName').val()+"&passWord="+$('#password').val(),
       cache:false,
       success: function(msg)
           {            
            if(msg =='success'){
            //redirect to dashboard
            }
            if(msg == "failure"){
            //display login error message               
           }
     });

This is a basic example how the thing should work. You can of course extend it or make it more robust.