Jump to content

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.

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/277178-codeigniter-hook-infinite-redirect-loop/
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

$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

Edited by Mahngiel

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

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.

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

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
	}
}

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?

Edited by Mahngiel

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'

Edited by ignace
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.