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.

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

报告相同问题?

悬赏问题

  • ¥35 平滑拟合曲线该如何生成
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 自己瞎改改,结果现在又运行不了了
  • ¥15 链式存储应该如何解决
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站