PHP uses CI to implement URL permission control with hooks -------- [Badboy], cibadboy

Source: Internet
Author: User

PHP uses CI to implement URL permission control with hooks -------- [Badboy], cibadboy

<Span style = "background-color: rgb (247,252,255); font-family: Verdana, Arial, Helvetica, sans-serif; "> </span> <p> <span style =" font-family: Verdana, Arial, Helvetica, sans-serif;
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. </Span> </p> <span style = "font-family: Verdana, Arial, Helvetica, sans-serif;
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.
</Span> </p> <span style = "font-family: Verdana, Arial, Helvetica, sans-serif;
Font-size: 14px;
Background-color: rgb (247,252,255 );
"> Code: </span> </p> <pre name =" code "class =" php "> <span style =" background-color: rgb (247,252,255 ); font-family: Verdana, Arial, Helvetica, sans-serif; ">
</Span>
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']);
}
}
}
I want to create a permission, using url-controlled (such as news/list, taking the controller and method name in the url) php CodeIgniter

If the framework does not provide a controller or method name, you need to write a URL address to obtain the controller and method name, then, you can use this permission to query data tables and put your own functions into _ construct () of each class for execution.

When a URL contains other parameters, how does one use the php ci framework paging class?

Other students want to add"
A total of X records of N/X pages "and other words are actually very simple, as long as the following code is added to the CI paging class File system \ libraries \ Pagination. php:

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.