Jump to content


Photo

codeigniter hook - infinite redirect loop


  • Please log in to reply
11 replies to this topic

#1 stijn0713

stijn0713

    Advanced Member

  • Members
  • PipPipPip
  • 231 posts

Posted 22 April 2013 - 10:57 AM

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.

 

 

 



#2 Mahngiel

Mahngiel

    Advanced Member

  • Members
  • PipPipPip
  • 1,076 posts

Posted 22 April 2013 - 11:02 AM

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:


?????????????????? This is a signature ????????????????????????

What do you  mean "so please write the code for me . . . "? We aren't here in gleeful anticipation just waiting to write code for you, you know.

CodeIgniter Libraries: Image Lib | Paginator Lib | AJAX Upload

#3 stijn0713

stijn0713

    Advanced Member

  • Members
  • PipPipPip
  • 231 posts

Posted 22 April 2013 - 12:30 PM

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



#4 Mahngiel

Mahngiel

    Advanced Member

  • Members
  • PipPipPip
  • 1,076 posts

Posted 22 April 2013 - 12:39 PM

$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, 22 April 2013 - 12:41 PM.

?????????????????? This is a signature ????????????????????????

What do you  mean "so please write the code for me . . . "? We aren't here in gleeful anticipation just waiting to write code for you, you know.

CodeIgniter Libraries: Image Lib | Paginator Lib | AJAX Upload

#5 stijn0713

stijn0713

    Advanced Member

  • Members
  • PipPipPip
  • 231 posts

Posted 22 April 2013 - 12:50 PM

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



#6 Mahngiel

Mahngiel

    Advanced Member

  • Members
  • PipPipPip
  • 1,076 posts

Posted 22 April 2013 - 01:14 PM

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.


?????????????????? This is a signature ????????????????????????

What do you  mean "so please write the code for me . . . "? We aren't here in gleeful anticipation just waiting to write code for you, you know.

CodeIgniter Libraries: Image Lib | Paginator Lib | AJAX Upload

#7 stijn0713

stijn0713

    Advanced Member

  • Members
  • PipPipPip
  • 231 posts

Posted 22 April 2013 - 01:28 PM

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



#8 Mahngiel

Mahngiel

    Advanced Member

  • Members
  • PipPipPip
  • 1,076 posts

Posted 22 April 2013 - 01:38 PM

 won't work i think

 

Have you tried?


?????????????????? This is a signature ????????????????????????

What do you  mean "so please write the code for me . . . "? We aren't here in gleeful anticipation just waiting to write code for you, you know.

CodeIgniter Libraries: Image Lib | Paginator Lib | AJAX Upload

#9 stijn0713

stijn0713

    Advanced Member

  • Members
  • PipPipPip
  • 231 posts

Posted 22 April 2013 - 01:44 PM

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


#10 Mahngiel

Mahngiel

    Advanced Member

  • Members
  • PipPipPip
  • 1,076 posts

Posted 22 April 2013 - 01:59 PM


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, 22 April 2013 - 01:59 PM.

?????????????????? This is a signature ????????????????????????

What do you  mean "so please write the code for me . . . "? We aren't here in gleeful anticipation just waiting to write code for you, you know.

CodeIgniter Libraries: Image Lib | Paginator Lib | AJAX Upload

#11 ignace

ignace

    Now mod flavored

  • Moderators
  • 6,253 posts
  • LocationBelgium

Posted 22 April 2013 - 02:53 PM

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, 22 April 2013 - 02:57 PM.


#12 stijn0713

stijn0713

    Advanced Member

  • Members
  • PipPipPip
  • 231 posts

Posted 23 April 2013 - 04:15 PM

Thanks ignace, solved!




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

Cheap Linux VPS from $5
SSD Storage, 30 day Guarantee
1 TB of BW, 100% Network Uptime

AlphaBit.com