PHP using the CI with hooks to implement URL permission control _php tutorial

Source: Internet
Author: User

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 Heweilun

http://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: ...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.