Home php教程 php手册 PHP之运用CI用钩子实现URL权限控制

PHP之运用CI用钩子实现URL权限控制

Jun 13, 2016 am 10:48 AM
2 background-color php rgb span style url accomplish control Permissions use use hook

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						
Copy after login
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

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

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

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

How do you parse and process HTML/XML in PHP? How do you parse and process HTML/XML in PHP? Feb 07, 2025 am 11:57 AM

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

gateio exchange official website entrance Sesame door gate official website entrance gateio exchange official website entrance Sesame door gate official website entrance Feb 21, 2025 pm 02:48 PM

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.

PHP Program to Count Vowels in a String PHP Program to Count Vowels in a String Feb 07, 2025 pm 12:12 PM

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

gate official website login portal gate.io login web version gate official website login portal gate.io login web version Feb 18, 2025 pm 03:00 PM

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.

7 PHP Functions I Regret I Didn't Know Before 7 PHP Functions I Regret I Didn't Know Before Nov 13, 2024 am 09:42 AM

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

Top 10 trading app virtual currency platforms rankings: Top 10 trading platforms in 2025 Top 10 trading app virtual currency platforms rankings: Top 10 trading platforms in 2025 Feb 17, 2025 pm 04:00 PM

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.

See all articles