The hook function of CI allows you to change or add core functions of the system without modifying the core files of the system.
For example, you can run a specific script or trigger your script at another time before the controller just loads or just loads.
Look at the code:
Add a hook declaration in system/application/config/hooks.php:
[PHP]
$hook [' post_controller_constructor '] = Array (
' Class ' = ' Acl ',
' function ' = ' filter ',
' filename ' = ' acl.php ',
' FilePath ' = ' hooks ',
);
system/application/config/config.php Hook System in effect
$config [' enable_hooks '] = TRUE;
Then create a new acl.php permission system profile in, and you can also put it in the database.
Visitor Permissions Mapping
$config [' ACL '] [' visitor '] = Array (
' = = Array (' index '),//home www.2cto.com
' Music ' = = Array (' index ', ' list '),
' User ' = = Array (' index ', ' login ', ' register ')
);
Administrator
$config [' ACL '] [' admin '] = Array (
);
-------------prompt for insufficient permission to configure and jump URL------------------//
$config [' Acl_info '] [' visitor '] = Array (
' Info ' = ' need to log in to continue ',
' Return_url ' = ' user/login '
);
$config [' Acl_info '] [' more_role '] = Array (
' Info ' = ' requires higher privileges to continue ',
' Return_url ' = ' user/up '
);
/* End of File acl.php */
/* location:./application/config/acl.php */
Add acl.php logical processing file under System/application/hooks directory
Class ACL
{
Private $url the modules accessed by _model;//, such as: music
Private $url methods accessed by _method;//, such as: Create
Private $url _param;//url with parameters that may be 1 or id=1&name=test
Private $CI;
function Acl ()
{
$this->ci = & Get_instance ();
$this->ci->load->library (' Session ');
$url = $_server[' php_self ');
$arr = explode ('/', $url);
$arr = Array_slice ($arr, Array_search (' index.php ', $arr) + 1, COUNT ($arr));
$this->url_model = isset ($arr [0])? $arr [0]: ";
$this->url_method = Isset ($arr [1])? $arr [1]: ' Index ';
$this->url_param = Isset ($arr [2])? $arr [2]: ";
}
function filter ()
{
$user = $this->ci->session->userdata (' user ');
if (Emptyempty ($user)) {//Visitor visitor
$role _name = ' visitor ';
} else {
$role _name = $user->role;
}
$this->ci->load->config (' ACL ');
$acl = $this->ci->config->item (' ACL ');
$role = $acl [$role _name];
$acl _info = $this->ci->config->item (' Acl_info ');
if (array_key_exists ($this->url_model, $role) && In_array ($this->url_method, $role [$this->url_ Model]) {
;
} else {//no permissions, give hint, jump URL
$this->ci->session->set_flashdata (' info ', $acl _info[$role _name][' info ');
Redirect ($acl _info[$role _name][' Return_url ');
}
}
}
Excerpt from I am Heweilunhttp://www.bkjia.com/PHPjc/478303.html www.bkjia.com true http://www.bkjia.com/PHPjc/478303.html techarticle span Style=background-color:rgb (247, 252, 255); Font-family:verdana, Arial, Helvetica, Sans-serif;/spanpspan style =font-family:verdana, Arial, Helvetica, Sans-serif; Font-size: ...