2011-06-16 67 views
0

我嘗試創建一個新的lib它會自動每一個控制器被加載和驗證,看是否有用戶登錄笨自定義庫

我自動加載腳本和它的加載時間細負載如何 - 以往任何時候都不會縫驗證

我的庫

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();   
    } 
} 

}

任何建議都非常讚賞

回答

2

我看到你正在嘗試做什麼,我會建議一種不同的方法。

創建MY_Controller並將其放入核心文件夾中。這裏是該文件中應該包含的內容的基本表示。

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_Controller,否則擴展Public_Controller。