Welcome Guest, Not a member yet? Register   Sign In
Error redirect from hook to another controller
#1

I want redirect user in login page if is not logged

my hook HK_Login.php inside application/hooks

 

   
Code:
<?php
       
       class HK_Login
       {
           private $CI;
           public function checkLogin()
           {
               $this->CI =& get_instance();
               if($this->CI->session->userdata('ID') == '' && $this->CI->uri->segment(3) != 'login' )
               {
                   redirect($this->CI->uri->segment(1) . '/secure/login');
               }
       
           }
       }


my controller Secure.php inside application/controllers
Code:
   <?php
   class Secure extends CI_Controller
   {
       public function __construct()
       {
           parent::__construct();
       }
       
       public function login()
       {
           $this->load->view('login/index');
       }
   }



My .htaccess

Code:
   DirectoryIndex index.php
   RewriteEngine on
   RewriteCond $1 !^(index\.php|images|css|js|robots\.txt|favicon\.ico)
   RewriteCond %{REQUEST_FILENAME} !-f
   RewriteCond %{REQUEST_FILENAME} !-d
   RewriteRule ^(.*)$ ./index.php/$1 [L,QSA]

A part of my config.php


Code:
   $config['index_page'] = '';
   $config['uri_protocol'] = 'REQUEST_URI';
   $config['enable_hooks'] = TRUE;


autoload

Code:
   $autoload['helper'] = array('url', 'custom');
   $autoload['libraries'] = array('database', 'session');

hook config


Code:
   $hook['post_controller_constructor'][] = array(
       'class' => 'HK_Login',
       'function' => 'checkLogin',
       'filename' => 'HK_Login.php',
       'filepath' => 'hooks'
   );


This is my routes.php


Code:
   $route['default_controller'] = 'dashboard';
   $route['404_override'] = '';
   $route['translate_uri_dashes'] = FALSE;
   $route['/secure/login'] = 'secure/login';


The result is :



Code:
   Not Found
   
   The requested URL /web/secure/login was not found on this server.



Why give me this error?
Reply
#2

(This post was last modified: 08-21-2015, 12:05 PM by CroNiX.)

post_controller_constructor will run AFTER the controller runs, hence "post" in the name. Not the place to be checking for authentication since it will check auth after the controller runs, allowing the person to do whatever they want.

You also really don't need to use a hook for this. Just create a base controller, MY_Controller, and have all of your other controllers that require auth checking extend that instead of CI_Controller.

I suspect your real problem though is the use of $this->CI->uri->segment(1) in your redirect: redirect($this->CI->uri->segment(1) . '/secure/login');
It appears that segment(1) is "web", so it redirects to web/secure/login, but you don't have a route for that. Your route is just "secure/login". Probably just remove the reference to segment(1) there.

/application/core/MY_Controller.php
PHP Code:
class MY_Controller extends CI_Controller {
  public function 
__construct()
  {
     
parent::__construct();
     
$this->checkLogin();
  }

  public function 
checkLogin()
  {
    
//your auth checking, if not logged in redirect to login page...
  
}


Then for all of your other controllers that require auth just extend MY_Controller...

/application/controllers/Other_class.php
PHP Code:
class Other_class extends MY_Controller {
  public function 
__construct()
  {
    
parent::__construct();
  }

  
//other regular class methods below...


Some reading: http://www.codeigniter.com/user_guide/ge...core-class
Reply
#3

Because you don't have /web/secure/login. It seems you don't have `web` controller.

> The requested URL /web/secure/login was not found on this server.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB