phpcodeigniterhttp-redirect

Conditional redirect() calls in CodeIgniter are not working as intended


The problem is that I am redirecting my function from one controller to another and I don't know why it isn't getting redirected there but is straightly getting out to the login page(RegistrationController) when I use the redirect function but when I load the view then it works.

Consider the following snippet. I want it to be redirected to the AdminController but it isn't getting redirected there. I have a view in the AdminController.

Beside this, the links in that view are also not working, they are also loading the RegisterationController whenever gets clicked.

class RegistrationController extends CI_Controller
{
    public function validate_credentials()
    {
        $username = $this->input->post('hotelEmail');
        $password = $this->input->post('hotelPassword');

        $this->load->model('AdminModel');
        $query = $this->AdminModel->validate($username, $password);
  
        if ($query) // If the user's credentials validated . . .
        {
            $data = array(
                'hotel_email'   => $this->input->post('hotelEmail'),
                'hotel_id'  => true
            );
            $this->session->set_userdata($data);
            redirect('AdminController');
        } else {
            $this->session->set_flashdata('wrong_credentials', 'Wrong Username or Password!');
            $this->load->view('login');
        }
    }

The class of the view controller is extended from the AuthenticationController which is as following

class AuthenticationController extends CI_Controller{
    public function __construct(){
        parent::__construct();
        $this->checkSession();
    }

    public function checkSession(){
        if(!$this->session->set_userdata('hotel_id')){
            redirect('RegistrationController/');
            exit();
        }
    }
}

Solution

  • remove the set_ from set_userdata from the checkSession() function and you are good to go!