CodeIgniter: trying to call constructor method to check if user is logged in (causes endless redirection loop)

夙愿已清 提交于 2019-12-11 11:44:41

问题


I've got a Problem with CodeIgniter 2.1.2 and stuck for hours try to solve it :-/

I know there are plenty(!) of threads about that, but i couldn't find a solution for my problem.

I want to load a method in my constructor to check if a user is logged in, so i tried this:

class my_class extends CI_Controller
{
public function __construct();
parent::__construct();

$this->check(); //doesn't work, endless redirection loop
}

"check()" is:

public function check()
{
    if (! $this->session->userdata('logged_in'))  
    {     
        redirect('/login'); // tried with & without '/'
    }
}

method "login()" looks like this:

public function login()
{
  ...
  //do form validation stuff and on success:
  if ($this->form_validation->run() == TRUE)
    {
     $this->session->set_userdata('logged_in', TRUE);
     redirect('/entry'); 
    }
   //load login_view
}

method "entry()":

public function entry()
{
  //$this->check();//Old (redundant) Version

  //Authorized and Happy...
}

// some other methods, also call the check() method first

So my question is, why the calls in the methods works (but with much redundant calls) and the call in the constructor give me an endless loop? Do I miss something?? (I also didn't change stuff in the htaccess files..)

Thx in advance :-)


回答1:


To elaborate on what commenters have said:

Ignoring the MY_Controller solution entirely for a moment, the issue is that your __construct() method -- and subsequently, $this->check() -- is being called on every controller, including your login controller.

  1. Load homepage. Constructor loads check().
  2. Not logged in. Redirect to /login.
  3. Login constructor loads check().
  4. Not logged in. Redirect to /login.

... and so on and so forth.

The solution is to check what URL is being accessed, and if it belongs to your login controller/method, then don't perform the redirect.

public function check()
{
    if ($this->uri->uri_string() !== 'login' && ! $this->session->userdata('logged_in'))
    {     
        redirect('login');
    }
}

Inversely, your login method should check if the user is indeed already logged in, and if so, redirect him to your homepage or something. But I digress.


The MY_Controller solution involves creating a base controller whose constructor performs the logged in check. Then, any controllers where you want to perform that check should extend MY_Controller instead of CI_Controller.

Note that your login controller will not extend MY_Controller, because you don't want to perform the check, or your infinite loop will show up again.




回答2:


It's difficult to be precise, but here goes.

I assume that login() is not a method in the my_class class/controller. If they are, this could be the cause of the error as the constructor would be called each time.

A better method would be to have a secure controller extend CI_Controller, e.g. Auth_Controller. This would be in application/libraries/MY_Controller.php file.

Your secure controllers would extend Auth_Controller, and the check() would be made in its contructor. The login controller would not extend Auth_Controller.




回答3:


here are some quick suggestions, but first make sure you autoload session library in config/autoload.php

this is not the login class. you would not have a redirect in the login class, you would just go straight to $this->enterLogin() or whatever the method name is, so that it does not loop.

 // Capitalize the first letter of the class!!
 class My_class extends CI_Controller
 {
     public function __construct();
     parent::__construct();

     // check if login is false
     // redirect here in the constructor, not buried in a method
     // note the underscore in the name _checkLogin() to make that method private

     if ( $this->_checkLogin() == false ) 
    { redirect( '/login/', 'refresh' );  }

 } // end constructor


 // private function 
 function _checkLogin(){
    if (! $this->session->userdata('logged_in'))  
    {   return false ;  }
    else
    { return true; } 

  } // 


来源:https://stackoverflow.com/questions/15414049/codeigniter-trying-to-call-constructor-method-to-check-if-user-is-logged-in-ca

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!