0
votes

i'm having a couple of problems in url routing in codeigniter. I've my login controller which loads login view file for taking user input and i'm using ajax login that returns me only base_url. i'm using another controller instead of login controller in my routes.php so ajax returns can't redirect me to the admin dashboard. In my Login controller, index function is:

 public function index() {
     if ($this->session->userdata('admin_login') == 1)
        redirect(base_url() . 'index.php?admin/dashboard', 'refresh');
     if ($this->session->userdata('teacher_login') == 1)
        redirect(base_url() . 'index.php?teacher/dashboard', 'refresh');
    if ($this->session->userdata('student_login') == 1)
         redirect(base_url() . 'index.php?student/dashboard', 'refresh');
    if ($this->session->userdata('parent_login') == 1)
        redirect(base_url() . 'index.php?parents/dashboard', 'refresh');
    $this->load->view('backend/login');
}

here i'm using session to check admin and another user's credential to redirect them their particular dashboard.

and my login.php view file i'm trying to use this:

 <?php 
    $data['page'] = 'login';
    $this->session->set_userdata($data);
 ?>

and then in routes.php im trying to get seesion varable to redirect right controller.

    $CI =& get_instance();
     if( $CI->session->userdata('page') ){
     redirect(base_url().'login/');
    }

    $route['default_controller'] =   'Frontend';
    $route['404_override'] = '';
    $route['translate_uri_dashes'] = FALSE;

and i don't know why it's not working as i expected. Please tell me what i should do. Any help will highly be appreciated. Thanks in advance.

1
can i assume session in codeigniter routes.php file is impossible? @Bugfixer - Nirob

1 Answers

1
votes

Use the below coding

$config['enable_hooks'] = TRUE;

Create the one file (check_session.php) in hook folder

class Check_Session extends CI_Controller{


function CheckSession(){

     if( $this->session->userdata('page') ){
     redirect(base_url().'login/');
    }
        }
}

And the apply below coding in hook.php in config folder

$hook['pre_controller']     = array(
                                'class'    => 'Check_Session',
                                'function' => 'CheckSession',
                                'filename' => 'check_session.php',
                                'filepath' => 'hooks',
                                'params'   => ''
                             );

Feel free to ask, If you have any queries....