codeigniter检查控制器构造函数中的经过身份验证的用户

时间:2013-07-16 10:43:01

标签: php codeigniter

是否可以在代码点火器控制器的构造函数中检查用户认证?

用户控制器

class user extends CI_Controller{

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

    $this -> load -> model('user_model');
    if (!$this -> user_model -> logged_in()) {
        $this -> load -> view('user/login');

    } else {
        return TRUE;

    }
   }

}

当我运行这个时,登录后我登录了我的仪表板顶部。登录前我得到两个登录视图。我试过这个最小化反复检查用户登录与否。

2 个答案:

答案 0 :(得分:0)

如果要显示登录页面而不更改网址,则应该执行转发方法。但CI没有前进的方法。

检查以下答案。 Internal forward action in CodeIgniter

答案 1 :(得分:-1)

只需进行简单的会话检查,如果设置与否,则检查用户的会话有效性:

if(!$this->session->userdata('logged_user')){
    redirect('controller/function', 'refresh');
}

在控制器中:

public function function(){
    $this->load->view('login');
}