Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/github/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
Php 如何检查授予用户的用户权限(代码点火器)_Php_Database_Codeigniter - Fatal编程技术网

Php 如何检查授予用户的用户权限(代码点火器)

Php 如何检查授予用户的用户权限(代码点火器),php,database,codeigniter,Php,Database,Codeigniter,可能重复: 我有5个用户类型和权限表,在其中我为不同的用户授予不同的权限。权限,如查看、删除、添加等。用户根据这些权限访问该功能 我完成了数据库。我想在调用控制器之前检查每个页面上授予用户的权限 您应该将身份验证逻辑放入控制器的构造函数中 或 在基本控制器的构造函数中(更干燥,因为不必在所有控制器中重复逻辑)。我将创建一个扩展核心控制器的新控制器。将此文件放入application/core/ class MY_AuthController extends CI_Controller {

可能重复:

我有5个用户类型和权限表,在其中我为不同的用户授予不同的权限。权限,如查看、删除、添加等。用户根据这些权限访问该功能


我完成了数据库。我想在调用控制器之前检查每个页面上授予用户的权限

您应该将身份验证逻辑放入控制器的构造函数中


在基本控制器的构造函数中(更干燥,因为不必在所有控制器中重复逻辑)。

我将创建一个扩展核心控制器的新控制器。将此文件放入
application/core/

class MY_AuthController extends CI_Controller {
    public function __construct() {
        // Do your auth check in here, redirect if not logged in
    }
}
然后,所有需要验证的页面都将继承这个新控制器。此文件仅放在常规控制器文件夹中

class Admin extends MY_AuthController {
    // All your controller goodness in here..
}

我建议您阅读以下两篇文章:

1.菲尔·斯特金的帖子。 Phil将向您介绍如何创建父控制器,其构造函数将包含会话和潜在的数据库逻辑。此后创建的所有控制器都应从自定义控制器继承,而不是从本机
CI\u控制器继承

接着是

2.谢恩·皮尔森的。 Shane的文章改进了Phil的技术,将自定义控制器从
/core
重新定位到
/base
,并使用了更好的
\u autoload()
'er。例如,这个实现允许我使用CodeIgniter的CLI类,而Phil的CLI类却被窃听了


给你一个想法-你的代码完成后看起来有点像这样:

/base/MY\u In\u Controller.php中

在呼叫控制器之前?!但是,在没有控制器的情况下,您将如何在db中检查用户权限?@BhuvanRikka,通过制作一个通用模型。并在每次控制器呼叫前检查。或者用钩子。可能吗
<?php
class MY_In_Controller extends CI_Controller{
    function __construct(){
        parent::__construct();
        //things like:
        //is the user even logged in? thank heavens I don't have to check this in every controller now. redirect if the session doesnt exist.
        //query the database and grab the permissions for the user. persist them with $this->load->vars();
        $this->data['perms'] = some_database_function();
        $this->load->vars($this->data);
    }
}
<?php
class Manage extends MY_In_Controller{
    function __construct(){
        parent::__construct();
    }
    function index(){
        $this->load->view('manage');
        //and I can still access their permissions here and in the view.
        print_r($this->data['perms']);
    }
}