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 - Fatal编程技术网

CodeIgniter自定义库

CodeIgniter自定义库,codeigniter,Codeigniter,我正在尝试创建一个新的库,它将在每次加载控制器时自动加载,并进行身份验证以查看用户是否登录 我正在自动加载脚本,它的加载很好,但它无法进行身份验证 我的自由 class Authentication { var $CI; function Authenication() { $this->CI =& get_instance(); $this->CI->load->library('session'); $is_logged_in =

我正在尝试创建一个新的库,它将在每次加载控制器时自动加载,并进行身份验证以查看用户是否登录

我正在自动加载脚本,它的加载很好,但它无法进行身份验证

我的自由

class Authentication {

var $CI;
function Authenication() {

    $this->CI =& get_instance();

    $this->CI->load->library('session');
    $is_logged_in = $this->CI->session->userdata('is_logged_in');
    if(!isset($is_logged_in) || $is_logged_in != true)
    {
        echo 'You don\'t have permission to access this page. <a href="../login">Login</a>';    
        die();        
    }
}
类身份验证{
var$CI;
函数身份验证(){
$this->CI=&get_instance();
$this->CI->load->library('session');
$is_logged_in=$this->CI->session->userdata('is_logged_in');
如果(!isset($is_logged_in)| |$is_logged_in!=true)
{
echo“您没有访问此页面的权限”;
模具();
}
}
}


非常感谢您的任何建议

我明白您的意图,我建议您采用不同的方法

创建MY_控制器并将其放入core文件夹中。下面是该文件中应该包含的内容的基本表示形式

class Public_Controller extends CI_Controller 
{
    // The data array holds all of the information to be displayed in views
    public $data = array();

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

        // Authentication library handles sessions and authentication, etc...
        $this->load->library('Authentication');

        $this->data['account']['is_logged_in'] = $this->authenication->is_logged_in();
    }
}

class Auth_Controller extends Public_Controller
{   
    function __construct() 
    {
        parent::__construct();

        if ( !$this->data['account']['is_logged_in'] )
        {
            redirect('user/login', 'location');   
        }
    }
}
如果您这样做,那么对于需要身份验证的控制器,您可以只扩展Auth\u Controller,或者扩展Public\u Controller