dongpu6141 2016-06-02 14:35
浏览 72
已采纳

从构造函数调用函数时,Codeigniter视图执行两次

I followed a tutorial on how to set up a login system for php apps in codeigniter. The logic when the user has session data is working great, but I have a problem when the user isn't logged in (f.ex. refreshes the page after a while). The view of "not_logged_in" gets executed twice when I were to call for the functin from constructor. The following code works, but it means I gotta add the code for every function I add after.

class App extends CI_Controller {

 function __construct()
 {
      parent::__construct();



 }
 private function logged_in()
 {

      $is_logged_in = $this->session->userdata('is_logged_in');
      if (isset($is_logged_in) OR $is_logged_in)
      {
           return TRUE;
      }
      else
      {
           $data['title'] = 'Chyba přihlášení';
           $data['main_content'] = 'not_logged_in';
           $this->load->view('includes/template', $data);
           return FALSE;
      }
 }

 function index()
 {
      if($this->logged_in())
      {
           $data['title'] = 'APLIKACE';
           $data['main_content'] = 'app_view';
           $data['userid'] = $this->session->userdata('userid'); //get userid from session
           $this->session->unset_userdata('userid'); //destroy the data
           $this->load->view('includes/template' , $data);
      }
 }


 function logout()
 {
     $this->session->sess_destroy();
     redirect('login');
 }


}

Now the real question, how would I go about putting the whole logic into a constructor without having to check for it in every function?

  • 写回答

1条回答 默认 最新

  • duanjiancong4860 2016-06-02 21:14
    关注

    Make APPPATH.'core/MY_Controller.php' file and put authentication logic in constructor there. Than extend that class from every controller (you need auth logic).

    class MY_Controller extends CI_Controller
    {
        public function __construct();
        {
            parent::__construct();
            $this->check_login();
        }
    
        protected function check_login()
        {
            $is_logged_in = $this->session->userdata('is_logged_in');
    
            //here should be *AND* instead *OR* logic
            if (isset($is_logged_in) && !empty($is_logged_in))
            {
                return TRUE;
            }
            else
            {
                redirect('login/index');
                exit();
            }
        }
    }
    

    Login.php controller:

    class Login extends CI_Controller//NOT extending MY_Controller to avoid infinite loop
    {
        public function __construct();
        {
            parent::__construct();
        }
    
        public function index()
        {
            //here is login view
            //and logic of preserving session
            //with redirect to 'app/index' after successful login
        }
    
        public function logout()
        {
            $this->session->sess_destroy();
            redirect('login');
        }
    }
    

    App.php controller:

    class App extends MY_Controller//extending MY_Controller to check login status
    {
        public function __construct();
        {
            parent::__construct();
        }
    
        public function index()
        {
            //here is app dashboard view
        }
    
        public function statistics()
        {
            //here is some other method that requires logged in user
        }
    }
    

    I also would recommend you to check Ion_auth authentication system to see if suitable for you.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 merge函数占用内存过大
  • ¥15 Revit2020下载问题
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 单片机无法进入HAL_TIM_PWM_PulseFinishedCallback回调函数
  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.