简体   繁体   中英

CodeIgniter check sessions in every controller?

I have define this controller to check user session:

class SessionController extends CI_Controller {

function __construct()
{
    parent::__construct();
    $this->is_logged_in();      
}

function dashboard_area(){
    $data['main_content'] = 'dashboardView';
    $this->load->view('dashboardTemplate/template', $data);         
}

function is_logged_in()
{
    $is_logged_in = $this->session->userdata('is_logged_in');
    if(!isset($is_logged_in) || $is_logged_in != true)
    {
        echo 'You don\'t have permission to access this page.';
        die();
        //$this->load->view('login_form');
    }
}       
    } ?>

and in my login controller I do this..

   class LoginController extends CI_Controller {

 function index(){      
    $new['main_content'] = 'loginView';
    $this->load->view('loginTemplate/template', $new);          
}   

function verifyUser(){      
    //getting parameters from view 
    $data = array(
            'username' => $this->input->post('username'),
            'password' => $this->input->post('password')
    );


    $this->load->model('loginModel'); 
    $query = $this->loginModel->validate($data);

          if ($query){             //if the user c validated
        //data variable is created becx we want to put username in session
            $data = array(
                'username' => $this->input->post('username'),
                 'is_logged_in' => true 
            );

         $this->session->set_userdata($data);
         redirect('sessionController/dashboard_area');
    }
    else
     {
        $this->index();
    }
}
function logout()
{
    $this->session->sess_destroy();
    $this->index();
}
}   
?>

now the problem is I have many controllers how to I use this .. I don't want to make new session controllers again and again ... and if there is any more good for handling sessions in more then one controller then please let me know .. iIhave seen this answer also ... but after applying the answer no 14 .. it gives me this error

Fatal error: Class 'MY_Controller' not found in C:\xampp\htdocs\StockManagmentSystem\application\controllers\categoryController.php on line 4

I am working in a latest code igniter

Create a MY_Controller in the application/core folder which extends CI_Controller. Place your session functions in the MY_Controller file and then have the rest of your controllers extend MY_Controller instead of CI_Controller.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM