0

I am trying to redirect to controller index if not authorized the access to other functions within same controller. According to my coding it is looking like infinite loop. Please help me to do that.

class Customer_Dashboard extends CI_Controller {
    public function __construct() {
        $method= $this->router->fetch_method();
        if ($this->session->userdata("cus_sel_comp")) {

        }else{
            if($method !="index"){
                redirect(base_url()."customer_dashboard");exit;
            }
        }
    }
    public function index() {
        // Here do some operations and let the user to select company and update the "cus_sel_comp" session variable. After set that session user can access the other controller functions.
    }
    public function other_function1() {

    }
    public function other_function2() {

    }
}

My coding is as above. I need to do this using same controller. Problem is if that session not set there is a infinite loop.

Blue
  • 22,608
  • 7
  • 62
  • 92
Gayan Fernando
  • 581
  • 1
  • 7
  • 23

2 Answers2

0

Instead of redirecting return index function. See the code below

 if($method !="index"){
              return $this->index();
 }
Srishin Kp
  • 152
  • 2
  • 7
0

You are calling the same function and redirecting it to same method.

    class Customer_Dashboard extends CI_Controller {
        public function __construct() {
            $method= $this->router->fetch_method();
            if ($this->session->userdata("cus_sel_comp")) {

            }else{
                if($method !="index"){
                    redirect(base_url()."Customer_Dashboard/index"); // Redirect it to index if other method is invoked.
                }
            }
        }
        public function index() {
            // Here do some operations and let the user to select company and update the "cus_sel_comp" session variable. After set that session user can access the other controller functions.
        }
        public function other_function1() {

        }
        public function other_function2() {

        }
    }

Also dont use base_url() instead of that define an path in config base_url() has many other entries present which are un-necessarily called.

Nishant Nair
  • 1,999
  • 1
  • 13
  • 18