构造/无限循环中的Codeigniter登录检查问题 [英] Codeigniter Login check issue in construct / infinite loop

查看:95
本文介绍了构造/无限循环中的Codeigniter登录检查问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要检查是否已登录用户.

i need to check is user loged or not.

我在控制器中有许多功能,因此我在构造函数中对其进行了检查.但它进入了无限循环.

i have to many function in controller, so i check it in construct function. but its entering infinite loop.

问题是:无限循环.

  function __construct()
    {
        parent:: __construct();
        $this->is_logged_in();
        $this->clear_cache();
    }



 function is_logged_in()
{
            if( !class_exists('CI_Session') ) $this->load->library('session');

    if( $this->session->userdata('login') )
    {
        $data['name'] = $this->session->userdata('username');       

    }
    else
    {
        redirect(base_url('admin/login'));
    }

}

我不想在所有功能/页面中都使用$this->is_logged_in().

i dont want to use $this->is_logged_in() in all functions/pages.

推荐答案

如果您不想在application/core的所有函数或页面上使用它,则可能要创建一个核心控制器,然后在此处进行检查./p>

You might want to create a core controller then do the checking there if you don't want to use it on all your functions or pages on application/core

class MY_Controller Extends CI_Controller
{
    public function __construct()
    {
        parent::__construct();
    }

    protected function _is_logged_in()
    {
        if( !class_exists('CI_Session') ) $this->load->library('session');

        if( $this->session->userdata('login') )
        {
        $data['name'] = $this->session->userdata('username');       

        }
        else
        {


       if ($this->uri->segment(2) == 'admin' && $this->uri->segment(3) !== 'login')
        redirect(base_url('admin/login'));
        }
    }
}

然后将其扩展为:在要验证登录名的所有控制器上,也可以将 $this->_is_logged_in()直接在您的MY_Controller上起作用,强制对其进行扩展的所有控制器进行检查.由您决定

then extend it like: On all your controllers that you want to verify login, or you can put the $this->_is_logged_in() function directly at your MY_Controller forcing all controllers that extends it to be checked. It's Up to you

class admin Extends MY_Controller
{
    public function __construct()
    {
        parent::__construct();
        $this->_is_logged_in();
    }
}

class user Extends MY_Controller
{
    public function __construct()
    {
        parent::__construct();
        $this->_is_logged_in();
    }
}

我使用了protected,因此只有扩展了my_controller的类才能使用它,并在名称下添加un下划线,这样就无法通过URL对其进行访问

I used protected so that only classes that extends my_controller can use it, and add un underscore at the name, so it can't be accessed via url

这篇关于构造/无限循环中的Codeigniter登录检查问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆