【问题标题】:CodeIgniter - How to check session to be used at every methodsCodeIgniter - 如何检查要在每种方法中使用的会话
【发布时间】:2012-01-05 18:34:15
【问题描述】:

假设我的控制器名为 Book,我有很多方法,例如 get_book();read_book();remove_book();

没有用户登录就不能使用类中的任何方法,我可以从会话中获取user_id

我的问题是,检查user_id 会话是否已设置以便我可以使用这些方法的最佳方法是什么?

至于现在,我正在考虑创建一个 is_logged_in() 方法,并通过 if-else 语句将其应用于每个方法,例如

if($this->is_logged_in()
{
   //do something
}
else
{
   //redirect to home
}  

是不是又长又乏味?有没有最终的方法来实现这一目标?

我看了链接

codeigniter check for user session in every controller

但似乎我仍然必须在每个方法中应用is_logged_in 检查。

谢谢你帮助我!

【问题讨论】:

    标签: codeigniter session


    【解决方案1】:

    /application/core中创建一个名为MY_controller.php的文件(前缀可以在配置文件中编辑):

    <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
    
    class MY_Controller extends CI_Controller {
    
    
        function __construct()
        {
    
            parent::__construct();
    
            //Initialization code that affects all controllers
        }
    
    }
    
    
    class Public_Controller extends MY_Controller {
    
        function __construct()
        {
            parent::__construct();
    
            //Initialization code that affects Public controllers. Probably not much needed because everyone can access public.
        }
    
    }
    
    class Admin_Controller extends MY_Controller {
    
        function __construct()
        {
            parent::__construct();
            //Initialization code that affects Admin controllers I.E. redirect and die if not logged in or not an admin
        }
    
    }
    
    class Member_Controller extends MY_Controller {
    
        function __construct()
        {
            parent::__construct();
    
            //Initialization code that affects Member controllers. I.E. redirect and die if not logged in
        }
    
    }
    

    然后,无论何时创建新控制器,您都可以决定它需要什么访问权限

        class Book extends Member_Controller {
    
            //Code that will be executed, no need to check anywhere if the user is logged in. 
            //The user is guaranteed to be logged in if we are executing code here.
    
    //If you define a __construct() here, remember to call parent::__construct();
        }
    

    这大大减少了代码重复,因为如果您需要 Book 以外的其他成员控制器,您可以扩展 Member_Controller。而不必在所有这些中进行检查。

    【讨论】:

    • 我明白你的回答,这真的是遵循 DRY 并帮助我将正确的业务规则应用于不同的用户组。非常感谢您和@Kemal Kernal 的帮助:)
    【解决方案2】:

    您不一定需要这样做。只需将登录检查代码放入构造函数中即可!

    class Book extends CI_Controller
    {
        public function __construct()
        {
            if ($this->is_logged_in())
            {
                // redirect to home
            }
        }
    
        public function get_book()
        {
            ...
        }
    
        // The rest of the code...
    }
    

    【讨论】:

    • 非常感谢,我测试过,它完美无瑕。要从链接更新是我们应该将 MY_Controller 放在 application/core 下。再次感谢您的帮助 :) 祝您有美好的一天!
    • @user826224,你仍然需要用这个复制代码。我的答案与您链接的答案有很大不同,您应该仔细阅读:)
    【解决方案3】:

    您可以在控制器的构造函数中使用该方法,例如:

    if ( !$this->session->userdata('logged_in')) { 重定向(“登录”); }

    【讨论】:

      猜你喜欢
      • 2023-02-02
      • 1970-01-01
      • 1970-01-01
      • 2011-04-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-24
      • 1970-01-01
      相关资源
      最近更新 更多