0

I want user not to jump on my welcome page through URL so. I added session user data in the constructor but redirect is not working showing "ERR_TOO_MANY_REDIRECTS".. so I use header() and site_url and base_url too but not of these are working...

        class Admin extends MY_Controller
        {
        public function __construct()
        {
        parent::__construct();

            if (!$this->session->userdata('userId')) 
            return redirect('admin/index');
        }

            public function index()
            {


                $this->form_validation->set_rules('username', 'Username', 'required|alpha');
                $this->form_validation->set_rules('pass', 'password', 'required|max_length[12]');
                $this->form_validation->set_error_delimiters("<div class='text-danger'>","</div>");
                if($this->form_validation->run())
                {
                    $user=$this->input->post('username');
                    $pass=$this->input->post('pass');
                    $this->load->model('loginmodel');
                    $id=$this->loginmodel->isvalidate($user,$pass);
                    if($id)
                        {

                            $this->load->library('session');
                            $this->session->set_userdata('userId',$id);
                            return redirect('admin/welcome');

                        }
                    else
                        {
                            $this->session->set_flashdata('loginFailed','Invalid Username/password');
                            return redirect('admin/login');
                        }
                }
                else
                {
                    $this->load->view('admin/login');
                }
            }

        }

        ?>
ruchi
  • 17
  • 5
  • Apparently you are redirecting continuously to the same page or back and forth. For example redirect to admin/welcome and admin welcome redirects you back to admin/login. Or admin/login redirects you to admin/login etc. 1. Try to comment out the redirects en check what happens. 2. Debug everywhere before the redirect so you can see what's going on. 3. Uncomment one by one the redirects to see where the bug is. – Iason Apr 16 '20 at 14:52

1 Answers1

1

A constructor allows you to initialize an object's properties upon creation of the object. If you create a __construct() function, PHP will automatically call this function when you create an object from a class. (source)

In your code, admin redirect to admin (when the admin initialize, __construct() runs which causes to redirect to admin again.)

My suggestion: check sessions from the index method

public function index(){
    if(!$this->session->userdata('userId')){
       //code
    }else{
       $this->load->view('admin/login');
    }
}

Edit: Thanks to TimBrownlaw

You can redirect to another controller.

redirect('accounts/admin_login');
class Accounts extends MY_Controller {
    public function admin_login(){
        if($this->session->userdata('userId')){
             redirect('admin/index');
        }else{
            $this->load->view('admin/login');
         }
    }
}
Dum
  • 1,431
  • 2
  • 9
  • 23
  • 1
    Seeing as you are correctly redirecting to another method, which will prevent the OP's issue of being in an endless loop, that test could stay in the constructor which would "protect" all of the admin methods. Putting it inside the index method only protects access to the index method. – TimBrownlaw Apr 16 '20 at 16:33