stijn0713 Posted April 22, 2013 Share Posted April 22, 2013 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 More sharing options...
Mahngiel Posted April 22, 2013 Share Posted April 22, 2013 By the looks of it, you have an error in your code on line 17. Update that and you should be rockin' and rollin'! Link to comment https://forums.phpfreaks.com/topic/277178-codeigniter-hook-infinite-redirect-loop/#findComment-1425962 Share on other sites More sharing options...
stijn0713 Posted April 22, 2013 Author Share Posted April 22, 2013 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 Link to comment https://forums.phpfreaks.com/topic/277178-codeigniter-hook-infinite-redirect-loop/#findComment-1425970 Share on other sites More sharing options...
Mahngiel Posted April 22, 2013 Share Posted April 22, 2013 $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 Link to comment https://forums.phpfreaks.com/topic/277178-codeigniter-hook-infinite-redirect-loop/#findComment-1425973 Share on other sites More sharing options...
stijn0713 Posted April 22, 2013 Author Share Posted April 22, 2013 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 Link to comment https://forums.phpfreaks.com/topic/277178-codeigniter-hook-infinite-redirect-loop/#findComment-1425975 Share on other sites More sharing options...
Mahngiel Posted April 22, 2013 Share Posted April 22, 2013 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. Link to comment https://forums.phpfreaks.com/topic/277178-codeigniter-hook-infinite-redirect-loop/#findComment-1425977 Share on other sites More sharing options...
stijn0713 Posted April 22, 2013 Author Share Posted April 22, 2013 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 Link to comment https://forums.phpfreaks.com/topic/277178-codeigniter-hook-infinite-redirect-loop/#findComment-1425983 Share on other sites More sharing options...
Mahngiel Posted April 22, 2013 Share Posted April 22, 2013 won't work i think Have you tried? Link to comment https://forums.phpfreaks.com/topic/277178-codeigniter-hook-infinite-redirect-loop/#findComment-1425985 Share on other sites More sharing options...
stijn0713 Posted April 22, 2013 Author Share Posted April 22, 2013 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 } } Link to comment https://forums.phpfreaks.com/topic/277178-codeigniter-hook-infinite-redirect-loop/#findComment-1425986 Share on other sites More sharing options...
Mahngiel Posted April 22, 2013 Share Posted April 22, 2013 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? Link to comment https://forums.phpfreaks.com/topic/277178-codeigniter-hook-infinite-redirect-loop/#findComment-1425988 Share on other sites More sharing options...
ignace Posted April 22, 2013 Share Posted April 22, 2013 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' Link to comment https://forums.phpfreaks.com/topic/277178-codeigniter-hook-infinite-redirect-loop/#findComment-1425998 Share on other sites More sharing options...
stijn0713 Posted April 23, 2013 Author Share Posted April 23, 2013 Thanks ignace, solved! Link to comment https://forums.phpfreaks.com/topic/277178-codeigniter-hook-infinite-redirect-loop/#findComment-1426180 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.