登录后在codeigniter中重定向 [英] Redirect in codeigniter after login

查看:43
本文介绍了登录后在codeigniter中重定向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

成功登录后尝试进行重定向。登录信息使用ajax发送到控制器。

Trying to do a redirect after a successful login. The login info is sent using ajax to the 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)
        {
            redirect('welcome');

        }
    }
}

但是根本不工作。有人知道怎么了吗?

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

按要求问好我的html。我也在使用Twitter Bootstrap

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.

因此,当我单击登录按钮时,我的js代码将发送ajax请求

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

我的js代码如下

    /*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');
        }
    });
    });
});


推荐答案

使用ajax调用控制器方法时,需要将一些变量或值(以指示登录成功或失败)发回给进行了ajax调用的javascript以处理重定向。换句话说,由于您未对控制器进行调用,因此javascript应处理重定向通过直接发布到controller方法,而不是通过Ajax。

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.

登录后再重定向到welcome控制器后,ajax脚本将作为对ajax调用的响应输出的内容是控制器方法将重定向到的页面的整个HTML

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.

所以我建议,您应该在登录成功并发送后在控制器中设置一些会话值将一些值返回给ajax方法以指示登录成功或失败。

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.

然后您可以使用javascript进行重定向,并且在重定向后应该设置会话

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

Controller:

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脚本:

$.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.

这篇关于登录后在codeigniter中重定向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆