CodeIgniter 始终运行此代码

发布于 2024-12-13 12:02:04 字数 1131 浏览 0 评论 0原文

我需要对每个请求运行一些代码,始终取决于用户是否登录。

我该把这段代码放在哪里?

我是否有可能传递数据,这段代码:

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

        $this->load->helper(array('form', 'url'));
        $this->load->library('form_validation');
        $this->load->library('security');
        $this->load->library('tank_auth');
        $this->lang->load('tank_auth');
        $this->load->model('users_model');

        if ($this->tank_auth->is_logged_in())
        {
            $data = $this->users_model->get_userinfo($this->tank_auth->get_username());

            if ($data['exp'] >= $data['max_exp']) {

                $new_data = array(
                    'exp' => $data['exp'] - $data['max_exp'],
                    'level' => $data['level'] + 1,
                );

                $this->db->where('id', $data['id']);
                $this->db->update('users', $new_data);

                echo 'Hello?';
            }
        }
    }

这是 MY_Controller,在 Core 类中。我可以进一步传递这些数据吗?我想,在真实的课堂上重新获取所有数据感觉是不必要的。

I need to run a few code on every request, always depending on If the user is logged in or not.

Where do I put this code?

Is there any possibility I can pass the data, This code:

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

        $this->load->helper(array('form', 'url'));
        $this->load->library('form_validation');
        $this->load->library('security');
        $this->load->library('tank_auth');
        $this->lang->load('tank_auth');
        $this->load->model('users_model');

        if ($this->tank_auth->is_logged_in())
        {
            $data = $this->users_model->get_userinfo($this->tank_auth->get_username());

            if ($data['exp'] >= $data['max_exp']) {

                $new_data = array(
                    'exp' => $data['exp'] - $data['max_exp'],
                    'level' => $data['level'] + 1,
                );

                $this->db->where('id', $data['id']);
                $this->db->update('users', $new_data);

                echo 'Hello?';
            }
        }
    }

This is MY_Controller, in the Core class.. Can I pass this data further? I guess, regrabbing all data, over again in the real class, feels unneccerary.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

幽蝶幻影 2024-12-20 12:02:04

在 core 文件夹下创建一个类,然后让所有控制器扩展该类。

我做了一个与您所描述的非常相似的登录系统。

这是核心文件夹中的一个类:

class MY_Controller extends CI_Controller{
    public function __construct()
    {
        parent::__construct();
        $this->load->library('cart');
        $this->load->library('session');
        $this->load->library('pagination');

        $this->load->helper('form');
        $this->load->library('form_validation');
        if (!$this->session->userdata('loggedin')){
            redirect('/sessions/log_in/','refresh');
        } 
    }
}

注意:确保您的配置正确设置了继承前缀

然后控制器文件夹中的控制器将扩展 My_Controller

对于分层登录或更详细的示例,请参阅我的旧问题:

Codeigniter:通过继承控制登录权限

另外我的教程基于:

http://davidwinter。 me/articles/2009/02/21/authentication-with-codeigniter/

关于进一步传递数据的回答:
使用会话类?

http://codeigniter.com/user_guide/libraries/sessions.html

$this->load->library('session');
$this->session->userdata('fieldName') = 1;//*appropriateValue*;
//Call this in another class
echo $this->session->userdata('fieldName');

Create a class under the core folder then make all your controllers extend that class.

I did a log in system very similar to what you are describing.

This is a class in the core folder:

class MY_Controller extends CI_Controller{
    public function __construct()
    {
        parent::__construct();
        $this->load->library('cart');
        $this->load->library('session');
        $this->load->library('pagination');

        $this->load->helper('form');
        $this->load->library('form_validation');
        if (!$this->session->userdata('loggedin')){
            redirect('/sessions/log_in/','refresh');
        } 
    }
}

Note: make sure your config is correctly set up for inheritance prefix

Then your controllers in the controller folder will extend My_Controller

For tiered log-ins or a more detailed example see my old question:

Codeigniter: Controlling log in privileges with inheritance

Also the tutorial I based my stuff off of:

http://davidwinter.me/articles/2009/02/21/authentication-with-codeigniter/

Answer in regards to passing data further:
Use the session class?

http://codeigniter.com/user_guide/libraries/sessions.html

$this->load->library('session');
$this->session->userdata('fieldName') = 1;//*appropriateValue*;
//Call this in another class
echo $this->session->userdata('fieldName');
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文