Rumah > Artikel > pembangunan bahagian belakang > PHP之运用CI用钩子实现URL权限控制_PHP教程
CI 的钩子功能使得您可以在不修改系统核心文件的基础上来改变或增加系统的核心运行功能。
例如,您可以在控制器刚刚载入前或刚刚载入后来运行特定的脚本,或者在其他时刻来触发您的脚本。
看代码:
<span style="background-color: rgb(247, 252, 255); font-family: Verdana, Arial, Helvetica, sans-serif; "> <br> </span> <br> system/application/config/hooks.php中添加钩子声明:<br> [php]<br> <br> $hook['post_controller_constructor'] = array( <br> 'class' => 'Acl', <br> 'function' => 'filter', <br> 'filename' => 'acl.php', <br> 'filepath' => 'hooks', <br> ); <br> <br> system/application/config/config.php中让钩子系统生效 <br> <br> $config['enable_hooks'] = TRUE; <br> <br> 然后在中新建acl.php权限系统配置文件,当然你也可以放在数据库中。 <br> <br> <br> //游客权限映射 <br> $config['acl']['visitor'] = array( <br> '' => array('index'),//首页 www.2cto.com <br> 'music' => array('index', 'list'), <br> 'user' => array('index', 'login', 'register') <br> ); <br> //管理员 <br> $config['acl']['admin'] = array( <br> <br> ); <br> <br> //-------------配置权限不够的提示信息及跳转url------------------// <br> $config['acl_info']['visitor'] = array( <br> 'info' => '需要登录以继续', <br> 'return_url' => 'user/login' <br> ); <br> <br> $config['acl_info']['more_role'] = array( <br> 'info' => '需要更高权限以继续', <br> 'return_url' => 'user/up' <br> ); <br> <br> /* End of file acl.php */ <br> /* Location: ./application/config/acl.php */ <br> <br> system/application/hooks目录下添加acl.php逻辑处理文件 <br> <br> <br> class Acl <br> { <br> private $url_model;//所访问的模块,如:music <br> private $url_method;//所访问的方法,如:create <br> private $url_param;//url所带参数 可能是 1 也可能是 id=1&name=test <br> private $CI; <br> <br> function Acl() <br> { <br> $this->CI = & get_instance(); <br> $this->CI->load->library('session'); <br> <br> $url = $_SERVER['PHP_SELF']; <br> $arr = explode('/', $url); <br> $arr = array_slice($arr, array_search('index.php', $arr) + 1, count($arr)); <br> $this->url_model = isset($arr[0]) ? $arr[0] : ''; <br> $this->url_method = isset($arr[1]) ? $arr[1] : 'index'; <br> $this->url_param = isset($arr[2]) ? $arr[2] : ''; <br> } <br> <br> function filter() <br> { <br> $user = $this->CI->session->userdata('user'); <br> if (emptyempty($user)) {//游客visitor <br> $role_name = 'visitor'; <br> } else { <br> $role_name = $user->role; <br> } <br> <br> $this->CI->load->config('acl'); <br> $acl = $this->CI->config->item('acl'); <br> $role = $acl[$role_name]; <br> $acl_info = $this->CI->config->item('acl_info'); <br> <br> if (array_key_exists($this->url_model, $role) && in_array($this->url_method, $role[$this->url_model])) { <br> ; <br> } else {//无权限,给出提示,跳转url <br> $this->CI->session->set_flashdata('info', $acl_info[$role_name]['info']); <br> redirect($acl_info[$role_name]['return_url']); <br> } <br> } <br> } <br> <br> 摘自 I am heweilun <p align="left"></p><div style="display:none;"> <span id="url" itemprop="url">http://www.bkjia.com/PHPjc/478303.html</span><span id="indexUrl" itemprop="indexUrl">www.bkjia.com</span><span id="isOriginal" itemprop="isOriginal">true</span><span id="isBasedOnUrl" itemprop="isBasedOnUrl">http://www.bkjia.com/PHPjc/478303.html</span><span id="genre" itemprop="genre">TechArticle</span><span id="description" itemprop="description">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:...</span> </div> <div class="art_confoot"></div>