PHP之运用CI用钩子实现URL权限控制
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

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

As a leader in cryptocurrency trading, Gate.io offers a wide range of trading pairs, derivatives and financial services. The Chinese version of the website Sesame Open Door Gate is convenient for Chinese users and provides the same functions as Gate.io, but it is more suitable for Chinese people's habits. Users can access the Gate.io exchange or Sesame Open Gate official website through the designated website. Please be sure to keep your account information carefully and only visit the official website to ensure safety.

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

As a digital asset exchange, Gate.io provides users with a convenient login process. To log in to Gate.io, visit its official website and click the "Login" button in the upper right corner. Then enter your email address or mobile phone number and password and perform two-factor authentication (2FA) verification as needed. After successfully logging in, you can manage your account balance, orders, and transaction activity through the Gate.io dashboard. To ensure account security, it is recommended to use a strong password and change it regularly, while enabling 2FA and taking care of phishing attempts.

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

As of 2025, the world's leading virtual currency platforms include Coinbase, Binance, FTX, Kraken, Huobi, OKX, Gemini, KuCoin, Bybit and Bittrex. Coinbase has a wide range of cryptocurrency options and a user-friendly interface, while Binance offers a wide range of trading pairs and derivatives. FTX focuses on trading instruments and leverage options, while Kraken is known for its security, low fees and asset choices.
