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.

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

报告相同问题?

悬赏问题

  • ¥20 iOS绕地区网络检测
  • ¥15 python验证码滑块图像识别
  • ¥15 根据背景及设计要求撰写设计报告
  • ¥15 QT6颜色选择对话框显示不完整
  • ¥20 能提供一下思路或者代码吗
  • ¥15 用twincat控制!
  • ¥15 请问一下这个运行结果是怎么来的
  • ¥15 单通道放大电路的工作原理
  • ¥30 YOLO检测微调结果p为1
  • ¥15 DS18B20内部ADC模数转换器