Jump to content

Archived

This topic is now archived and is closed to further replies.

stijn0713

codeigniter hook - infinite redirect loop

Recommended Posts

I try to use a post_controller_constructor hook point to create a authentication. 

 

My problem is that i always get in an infinite loop.

 

So the flow goes basically like this:

 

 

in the codeigniter.php file the hook is called.

 

The hook has an authentication method that checks if 'logged_in' is set in the session, else redirects to a login controller, which displays the login view form, and sets the session after. Then i tried to redirect back to the hook.

 

I don't know what or where i have to redirect too after filling the session in.

 

It's just for the purpose of learning about hooks, so please don't suggest an alternative method.

 

 

 

Share this post


Link to post
Share on other sites

By the looks of it, you have an error in your code on line 17.  Update that and you should be rockin' and rollin'! :psychic:

Share this post


Link to post
Share on other sites

The hook is called here, in codeigniter.php

Is there a "post_controller_constructor" hook?
 * ------------------------------------------------------
 */
	$EXT->_call_hook('post_controller_constructor');

My hook:

class Auth {
    
    private $CI;
    
        function __construct(){
            $this->CI =& get_instance();
        }
    
    
        function authenticate(){
                
            $this->CI->session->set_userdata(array('hook' => $_SERVER['php_self']));    
            
            if(!$this->CI->session->userdata("logged_in")) {
                        
                redirect('user/login', 'location');
            }            
        }     
}

i try to redirect in hook to user controller, where i try todisplay login form

class User extends CI_Controller {

	public function __construct()
	{
		parent::__construct();
		
	}
	
	public function login(){
		
		$this->load->view('pages/login');
		$arr['un'] = $this->input->post('naam');
		$arr['pw'] = $this->input->post('paswoord');
		
		$this->session->set_userdata(array('logged_in' => $arr));
		
		redirect($this->session->userdata('hook'), 'location');
	}
}

after logging in, it should proceed back in the codeigniter.php file with the requested controller and method

Share this post


Link to post
Share on other sites

$this->session->set_userdata('logged_in' => TRUE) !== $this->session->set_userdata(array('logged_in' => TRUE));

 

Run a dump on $this->session->all_userdata() and you'll see what you're asking for doesn't exist

Share this post


Link to post
Share on other sites

Thanks for the reply. The point is, i cannot var_dump anything since firefox only alerts that there has been a request that will never terminate..

 

It's because i don't understand the navigation logic when using hooks in codeigniter.

 

say i request:   CI/news      it will call the news controller and output what's been set in the index() method . but now before getting to the news controller i want to put a hook to verify if one has logged in already, if not let them log in, and redirect back to the news/index

Share this post


Link to post
Share on other sites

you need to kill the script too, dude.  Although your "login" script doesn't actually log the user in, and you said this is just for practice... 

 

my advice is to change

$this->session->set_userdata(array('logged_in' => $arr));

to

$this->session->set_userdata('logged_in' => TRUE);

 

Then, when your hook is called,

if( $this->session->userdata('logged_in') )

will eval to true.

Share this post


Link to post
Share on other sites

that's not the problem... The real thing is.. if i just echo something in the method of the hook. it will echo it, go out of the hook and proceed with the next thing in codeigniter.php... HOWEVER, if i redirect within the method of the hook.. i don't know how to get back out of it... exit or die() won't work i think

Share this post


Link to post
Share on other sites

yes, with 'i think' i wanted to express that i have not full understanding of the navigation and therefore tried to exit or die() at a place maybe irrelevant. So i cannot say i die or exit() is not the way to go, however, for my understanding it's not the way how to get out of it. i tried to place it there:

lass User extends CI_Controller {

	public function __construct()
	{
		parent::__construct();
		
	}
	
	public function login(){
		
		$this->load->view('pages/login');
		$arr['un'] = $this->input->post('naam');
		$arr['pw'] = $this->input->post('paswoord');
		
		if(isset($arr)){
		$this->session->set_userdata('logged_in' , TRUE);
		}
		die(); // here
	}
}

Share this post


Link to post
Share on other sites

class Auth {
    
    private $CI;
    
        function __construct(){
            $this->CI =& get_instance();
            echo 'hook inst';
        }
    
    
        function authenticate()
        {
            echo 'method called';
                
            $this->CI->session->set_userdata(array('hook' => $_SERVER['php_self']));    

            echo $this->session->userdata('logged_in') ? 'logged' : 'not logged';
 
            die('end');
        }     
}

 

what happens?

Share this post


Link to post
Share on other sites

Obviously the infinite redirect loop is due to the hook being called for EVERY request thus also your login controller meaning it never reaches your controller. You should check your router to the current action/controller is NOT the login action.

 

Or maybe you should consider a framework that actually takes work out of your hands instead of putting more into it. Which is true where $frameworkName !== 'CodeIgniter'

Share this post


Link to post
Share on other sites

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.