Php 每个控制器中的CodeIgniter检查会话?

Php 每个控制器中的CodeIgniter检查会话?,php,codeigniter,session,methods,controller,Php,Codeigniter,Session,Methods,Controller,我已定义此控制器以检查用户会话: class SessionController extends CI_Controller { function __construct() { parent::__construct(); $this->is_logged_in(); } function dashboard_area(){ $data['main_content'] = 'dashboardView'; $this->load-&g

我已定义此控制器以检查用户会话:

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');
    }
}       
    } ?>
在我的登录控制器中,我这样做

   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();
}
}   
?>
现在的问题是我有很多控制器如何使用这个。。我不想一次又一次地创建新的会话控制器。。。如果在多个控制器中处理会话有任何更好的方法,请让我知道。。 我也看到了这个答案。。。但是在应用了第14个答案之后。。它给了我这个错误

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

我正在使用最新的代码点火器

在扩展CI_控制器的应用程序/核心文件夹中创建MY_控制器。将会话功能放在MY_Controller文件中,然后让其余控制器扩展MY_Controller而不是CI_Controller。

在扩展CI_Controller的应用程序/核心文件夹中创建MY_控制器。将会话功能放在MY_Controller文件中,然后让其他控制器扩展MY_Controller而不是CI_Controller。

谢谢,我已经完成了。。我知道我错在哪里了。实际上,我把MY_控制器放到了system core Folder中。虽然这种方法确实有效,但使用钩子可能值得一试,因为它可以防止对核心文件的“黑客攻击”。我暂时使用“黑客”一词,因为您没有修改任何一个,但添加了一个文件。谢谢,我已经这么做了。。我知道我错在哪里了。实际上,我把MY_控制器放到了system core Folder中。虽然这种方法确实有效,但使用钩子可能值得一试,因为它可以防止对核心文件的“黑客攻击”。我暂时使用黑客这个术语,因为您没有修改任何一个,而是添加了一个文件。