Codeigniter会话返回310重定向 [英] Codeigniter Session returning 310 redirect

查看:115
本文介绍了Codeigniter会话返回310重定向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个回调函数,检查我的登录详细信息是正确的 - 如果他们错了,它返回一个错误(这是工作正常)。如果细节正确,应该设置会话 $ this-> session-> set_userdata('logged_in',TRUE); ,然后继续使用<$ c



在我的函数索引()中,在任何信息中心页面上,{@ 都有



if($ this-> session-> userdata 'logged_in'))redirect('dashboard / home');



上面的行是导致我310



我想检查用户是否登录重定向到 dashboard / home

code> else返回登录页 home / login



strong>

  class Home extends CI_Controller {

function __construct(){
parent: :__构造();
}

public function index()
{
// if($ this-> session-> userdata('logged_in'))redirect仪表板/家);

$ data ['contentMangement'] = $ this-> options_model-> systemOptions();
$ data ['pageTitle'] ='登录';
$ data ['message'] =;
$ this-> load-> view('_ assets / header',$ data);
$ this-> load-> view('login',$ data);
$ this-> load-> view('_ assets / footer');
}

public function login(){
$ this-> form_validation-> set_rules('userEmail','Username','required | valid_email | trim | max_length | xss_clean');
$ this-> form_validation-> set_rules('userPassword','Password','required | trim | max_length [200] | xss_clean | callback__checkUsernamePassword');

if($ this-> form_validation-> run()=== FALSE){

$ data ['contentMangement'] = $ this-> options_model - > systemOptions();
$ data ['pageTitle'] ='登录';
$ data ['message'] = validation_errors('< div class =alert alert-error>','< / div>');
$ this-> load-> view('_ assets / header',$ data);
$ this-> load-> view('login',$ data);
$ this-> load-> view('_ assets / footer');

} elseif($ this-> form_validation-> run()=== TRUE){

redirect('dashboard / home');
}
}


函数_checkUsernamePassword(){

$ username = $ this-> input-> post userEmail');
$ password = $ this-> input-> post('userPassword');

$ user = $ this-> user_model-> check_login($ username,$ password);

if(!$ user)
{
$ this-> form_validation-> set_message('_ checkUsernamePassword','抱歉找不到您提供的详细信息') ;
return FALSE;
} else {
$ this-> session-> set_userdata('logged_in',TRUE);
return TRUE;
}
}
}


解决方案>

这里是发生了什么。



假设我在您的登录控制器中正确登录,设置 logged_in = TRUE ,并将我重定向到 dashboard / home 。在 index()函数 dashboard / home ,如果 logged_in = TRUE (它是),您将我重定向到 dashboard / home 。再次检查 logged_in = TRUE ,并重新导向我,等等。



正在导致无限重定向循环,导致310错误(太多重定向)。



您需要重新进行登录检查。在 / 中的 index()中,执行以下操作:



if($ this-> session-> userdata('logged_in')=== FALSE)redirect(site_url('dashboard / login'));



现在,当我访问仪表板主页时,如果我没有登录,您只会重定向我。这可以保护您的仪表板主页免受未经身份验证的用户,认证的用户进入无限循环。


I have a callback function that checks my login details are correct - If they are wrong it returns an error (this is working fine). If the details are correct it should set the session $this->session->set_userdata('logged_in',TRUE); and then continue with the function login and be redirected to the dashboard - This redirect works fine.

In my function index(){} on any dashboard pages have the line

if($this->session->userdata('logged_in')) redirect('dashboard/home');

The line above is the one that is causing my 310 redirect but I am unsure why?

I am wanting to check if the user is logged in redirect to dashboard/home else go back to the login page home/login

Controller:

class Home extends CI_Controller {

    function __construct() {
            parent::__construct();
        }    

    public function index()
    {
        //if($this->session->userdata('logged_in')) redirect('dashboard/home');

        $data['contentMangement'] = $this->options_model->systemOptions();
        $data['pageTitle'] = 'Login';
        $data['message'] = "";
        $this->load->view('_assets/header', $data);
        $this->load->view('login', $data);
        $this->load->view('_assets/footer');
    }

    public function login() {
          $this->form_validation->set_rules('userEmail','Username', 'required|valid_email|trim|max_length[99]|xss_clean');
            $this->form_validation->set_rules('userPassword','Password', 'required|trim|max_length[200]|xss_clean|callback__checkUsernamePassword');

            if($this->form_validation->run() === FALSE) {

                $data['contentMangement'] = $this->options_model->systemOptions();
                $data['pageTitle'] = 'Login';
                $data['message'] = validation_errors('<div class="alert alert-error">', '</div>');
                $this->load->view('_assets/header', $data);
                $this->load->view('login', $data);
                $this->load->view('_assets/footer');

            }elseif($this->form_validation->run() === TRUE){

            redirect('dashboard/home');
        }
    }


    function _checkUsernamePassword() {

            $username = $this->input->post('userEmail');
            $password = $this->input->post('userPassword');

            $user = $this->user_model->check_login($username,$password);

            if(! $user)
            {
                $this->form_validation->set_message('_checkUsernamePassword', 'Sorry the details you provided have not been found');
                return FALSE;
            }else{
                 $this->session->set_userdata('logged_in',TRUE);
                return TRUE;
            }   
    }
}

解决方案

Here's what's happening.

Assume, I login correctly, in your login controller, you set logged_in = TRUE, and redirect me to dashboard/home. In the index() function at dashboard/home, if logged_in = TRUE (which it is) you redirect me to dashboard/home again. Once again, you check logged_in = TRUE, and redirect me again, and so on and so forth.

This is causing an infinite redirection loop which causes the 310 Error (Too many redirects).

You'll need to rework your login check. In index() in dashboard/home, do this:

if ($this->session->userdata('logged_in') === FALSE) redirect(site_url('dashboard/login'));

Now, when I visit dashboard home, you only redirect me away if I'm not logged in. This protects your dashboard home against non-authenticated users, while not throwing authenticated users into an infinite loop.

这篇关于Codeigniter会话返回310重定向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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