检查这可能会帮助你
class MY_controller extends CI_controller{
function __construct() {
parent::__construct();
}
function _required_user($params =array()){
$action =$this->router->fetch_method();
if(empty($params['except']))
$params['except'] =array();
if(empty($params['only']))
$params['only'] =array();
if(count($params['except']) > 0 && in_array($action,$params['except']))
return true;
if(count($params['only']) > 0 && in_array($action,$params['only']) && $this->session->userdata('is_login'))
return true;
if($this->session->userdata('is_login'))
return true;
redirect('login');
}
}
class dashboard extends MY_Controller {
function __construct() {
parent::__construct();
$this->load->library('session');
$this->load->model('dbmodel');
$this->load->helper('url','form');
$this->_required_user(array('except'=>array('index')))
}
function add(){
/*
Session required
*/
}
function edit(){
/*
Session required
*/
}
function index(){
/*
no session required
*/
}
}
class content extends MY_Controller{
function __construct() {
parent::__construct();
$this->load->library('session');
$this->load->model('dbmodel');
$this->load->helper('url','form');
$this->_required_user(array('only'=>array('index')))
}
function add(){
/*
no Session required
*/
}
function edit(){
/*
no Session required
*/
}
function index(){
/*
session required
*/
}
}
class Myaccount extends MY_Controller{
function __construct() {
parent::__construct();
/*
for all functions session required
*/
$this->_required_user()
}
function edit(){
/*
session required
*/
}
function save(){
/*
session required
*/
}
}
>只有param:只有给定的函数/函数存在检查会话
>除了param:不要检查给定功能/功能的会话
>无参数:检查控制器中的所有功能和重定向的会话
您可以根据需要修改_reuired_user功能