代码签名检查所有功能(选项卡) [英] codeigniter login check for all functions(tabs)
问题描述
我有一个登录控件的功能。如果用户did not登录它重定向到 login.php
i have a function for login controls. if user didnt login it redirects to login.php
在我的控制器我有很多函数,页面。所以我必须在每个函数中调用 $ this-> is_logged_in();
函数。
in my controller i have to many functions and this functions represents pages in website. so do i have to call $this->is_logged_in();
function in each function.
例如:
class Admin extends CI_Controller{
function index(){
$this->is_logged_in(); // works fine like this
$this->load->view('admin/welcome_message');
}
function users(){
$this->is_logged_in(); // works fine like this
$this->load->view('admin/users');
}
function comments(){
$this->is_logged_in(); // works fine like this
$this->load->view('admin/comments');
}
}
我不想调用此函数在所有功能。当我调用这个结构中的结果是:无限循环。
i dont want to call this function in all function. when i call this in construct result is: infinite loop.
推荐答案
在你的应用程序/它的构造函数do:
Create your own base controller in your application/core folder and in its constructor do:
class MY_Controller extends CI_Controller {
public function __construct()
{
parent::__construct();
// Check that the user is logged in
if ($this->session->userdata('userid') == null || $this->session->userdata('userid') < 1) {
// Prevent infinite loop by checking that this isn't the login controller
if ($this->router->class != '<Name of Your Login Controller')
{
redirect(base_url());
}
}
}
}
所有的控制器只需要继承你的新控制器,然后所有的请求将检查用户是否登录。
Then all of your controllers just need to inherit your new controller and then all requests will check that the user is logged in.
这可以更具体如果需要也检查 $ this-> router->方法
匹配特定操作。
This can be more specific if required by also checking $this->router->method
to match against a specific action.
这篇关于代码签名检查所有功能(选项卡)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!