PHP uses CI to implement URL permission control with hooks

Source: Internet
Author: User
& Lt; spanstyle & quot; background-color: rgb (247,252,255); font-family: Verdana, Arial, Helvetica, sans-serif; & quot; & gt; & lt; /span & gt; & lt; p & gt; & lt; spanstyle & quo

Font-size: 14px;
Background-color: rgb (247,252,255 );
"> The CI hook function allows you to change or add the system's core running functions without modifying the system's core files.

Font-size: 14px; background-color: rgb (247,252,255); ">
For example, you can trigger your script before or after the controller is loaded, or at other times.

Font-size: 14px;
Background-color: rgb (247,252,255 );
"> Check the code:

 
 
Add the hook statement to system/application/config/hooks. php:
[Php]

$ Hook ['post _ controller_constructor '] = array (
'Class' => 'acl ',
'Function' => 'filter ',
'Filename' => 'acl. php ',
'Filepath' => 'Hooks ',
);
 
Make the hook system take effect in system/application/config. php

$ Config ['enable _ Hooks'] = TRUE;
 
Then, create the acl. php permission system configuration file in. of course, you can also put it in the database.
 

// Visitor permission ing
$ Config ['acl'] ['visitor '] = array (
''=> Array ('index'), // homepage www.2cto.com
'Music' => array ('index', 'list '),
'User' => array ('index', 'login', 'register ')
);
// Administrator
$ Config ['acl'] ['admin'] = array (

);

// ------------- Prompt message about insufficient permission configuration and jump url ------------------//
$ Config ['acl _ info'] ['visitor '] = array (
'Info' => 'login required to continue ',
'Return _ url' => 'User/login'
);

$ Config ['acl _ info'] ['more _ role'] = array (
'Info' => 'requires higher permissions to continue ',
'Return _ url' => 'User/up'
);

/* End of file acl. php */
/* Location:./application/config/acl. php */
 
Add the acl. php logical processing file under the system/application/hooks Directory
 

Class Acl
{
Private $ url_model; // The Accessed module, such as music.
Private $ url_method; // The Accessed method, for example, create
Private $ url_param; // The url parameter 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
$ 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 permission, prompt, jump url
$ This-> CI-> session-> set_flashdata ('info', $ acl_info [$ role_name] ['info']);
Redirect ($ acl_info [$ role_name] ['return _ url']);
}
}
}

From I am heweilun

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.