Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/codeigniter/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
构造/无限循环中的Codeigniter登录检查问题_Codeigniter_Login_Constructor_Codeigniter 2_Login Control - Fatal编程技术网

构造/无限循环中的Codeigniter登录检查问题

构造/无限循环中的Codeigniter登录检查问题,codeigniter,login,constructor,codeigniter-2,login-control,Codeigniter,Login,Constructor,Codeigniter 2,Login Control,我需要检查用户是否登录 我在控制器中有很多函数,所以我在构造函数中检查它。但它正在进入无限循环 问题是:无限循环 function __construct() { parent:: __construct(); $this->is_logged_in(); $this->clear_cache(); } function is_logged_in() { if( !class_exis

我需要检查用户是否登录

我在控制器中有很多函数,所以我在构造函数中检查它。但它正在进入无限循环

问题是:无限循环

  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\u logged\u in()

function __construct()
{
    parent:: __construct();
    if (($this->uri->segment(2) == 'admin') && ($this->uri->segment(3) != 'login'))
        $this->is_logged_in();
    $this->clear_cache();
}  
我相信有更好的方法,而且片段部分可能已经关闭,请查找更多信息。

@Allan

应该是

function __construct()
{
    parent:: __construct();
    if (!($this->uri->segment(2) == 'admin' && $this->uri->segment(3) == 'login'))
        $this->is_logged_in();
    $this->clear_cache();
} 

检查if条件。

如果不想在应用程序/core上的所有函数或页面上使用core控制器,则可能需要创建一个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->\u已登录()
函数直接在您的
MY\u控制器上执行,强制检查扩展它的所有控制器。由你决定

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访问它,它是哪个控制器?它是登录控制器还是登录控制器的父级?仍然是无限循环。这与我解决的第一个solutionloop相同,但现在您可以访问所有页面而无需登录。没有控制。@pekyolu maslak是否尝试过上述条件<代码>如果(!($this->uri->segment(2)='admin'&&&$this->uri->segment(3)='login'))