filter:

使用CI的hooks来实现filter.

1.在system/application/config/config.php中,把enable_hooks的值改为TRUE

$config['enable_hooks'] = TRUE;

2.在syste/application/config/hooks.php中,添加hooks,如下

$hook['post_controller_constructor'] = array(
    'class'    => 'SecurityFilterChain',
    'function' => 'do_filter',
    'filename' => 'security_filter_chain.php',
    'filepath' => 'hooks',
    'params'   => array(
        'logged_in_session_attr' => 'logged_in',
        'login_page' => '/login/',
        'should_not_filter' => array('/^//login$/', '/^//login//.*$/', '/^//user//profile.*$/'),
        'need_admin_role' => array('/^//user$/', '/^//user//.*$/', '/^//role$/', '/^//role//.*$/')
        )
    );

其中params 是传递给filter类的参数.

shoud_not_filter是不需要过滤的uri

need_admin_role是需要管理员角色的uri

3.生成文件system/application/hooks/security_filter_chain.php

class SecurityFilterChain { 
    function do_filter($params) 
    { 
        $CI = &get_instance(); 
        $uri = uri_string();

foreach($params['should_not_filter'] as $not_filter)
        {
            if(preg_match($not_filter, $uri) == 1)
            {
                return;
            }
        }
 
        if(!$CI->session->userdata($params['logged_in_session_attr'])) 
        { 
            redirect($params['login_page']); 
        }
        
        foreach($params['need_admin_role'] as $need_admin)
        {
            if(preg_match($need_admin, $uri) == 1)
            {
                $current_user = $CI->session->userdata('current_user');
                if(!isset($current_user['role_status']) or $current_user['role_status'] != 0) // 0表示管理员角色的id
                {
                    show_error('您没有权限访问这个页面', 403);
                    return;
                }
                break;
            }
        } 
 
    } 
}

helper

添加自定义的helper,名称为test

1.创建文件system/application/helpers/test_helper.php内容为:

<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

if ( ! function_exists('array_to_option'))
{
    function array_to_option($name, $data = array())
    {
        $html = "<select name=/"$name/">";
        foreach($data as $value => $text)
        {
            $html .= "<option value=/"$value/">$text</option>";
        }
        $html .= "</select>";
        return $html;
    }
}

2.加载这个helper

在autoload.php里边,autoload['helper']中添加test

$autoload['helper'] = array('url', 'form', 'test');

或者在controller的构造函数中添加

$this->load->helper('test')

3.使用。直接调用函数array_to_option即可

**CodeIgniter系列 添加filter和helper的更多相关文章

  1. [Asp.net MVC]Asp.net MVC5系列——添加模型

    目录 概述 添加模型 总结 系列文章 [Asp.net MVC]Asp.net MVC5系列——第一个项目 [Asp.net MVC]Asp.net MVC5系列——添加视图 概述 在本节中我们将追加 ...

  2. [Asp.net MVC]Asp.net MVC5系列——添加数据

    目录 概述 显示添加数据时所用表单 处理HTTP-POST 总结 系列文章 [Asp.net MVC]Asp.net MVC5系列——第一个项目 [Asp.net MVC]Asp.net MVC5系列 ...

  3. Asp.net MVC3 中,动态添加filter

    Asp.net MVC3 中,动态添加filter filter是attribute,不支持泛型,传入的参数必须是固定的值.总之很受attribute本身的限制. 发现一篇老外的文章,动态设置filt ...

  4. 通过添加filter过滤器 彻底解决ajax 跨域问题

    1.在web.xml添加filter <filter> <filter-name>contextfilter</filter-name> <filter-cl ...

  5. Jetty添加Filter过滤器

    1.Jetty嵌入到Spring项目 try { Server server = new Server(8080); WebAppContext context = new WebAppContext ...

  6. java项目中通过添加filter过滤器解决ajax跨域问题

    1.在web.xml添加filter <filter> <filter-name>contextfilter</filter-name> <filter-cl ...

  7. Springboot添加filter方法

    在springboot添加filter有两种方式: (1).通过创建FilterRegistrationBean的方式(建议使用此种方式,统一管理,且通过注解的方式若不是本地调试,如果在filter中 ...

  8. [Asp.net MVC]Asp.net MVC5系列——添加视图

    目录 系列文章 概述 添加视图 总结 系列文章 [Asp.net MVC]Asp.net MVC5系列——第一个项目 概述 在这一部分我们添加一个新的控制器HelloWorldController类, ...

  9. cas sso单点登录系列1_cas-client Filter源码解码(转)

    转:http://blog.csdn.net/ae6623/article/details/8841801?utm_source=tuicool&utm_medium=referral /* ...

随机推荐

  1. JSP2 特性

    JSP2 新特性 1.直接配置 JSP 属性 2.表达式语言 3.简化的自定义标签 API 4.Tag 文件语法 如果要使用 JSP2 语法,web.xml 文件必须使用 Servlet2.4 以上版 ...

  2. STL源码分析-rotate

    http://note.youdao.com/noteshare?id=4ba8ff81aa96373ba11f1b82597ec73a

  3. hdu 2608 (数论)

    hdu2608  0 or 1 题意:给你一个数N(N < 2^31), 问从 1--N 所有数的因子和S(N),求 S(N)%2 的值. 链接:http://acm.hdu.edu.cn/sh ...

  4. [Java多线程]-Thread和Runable源码解析

    多线程:(百度百科借一波定义) 多线程(英语:multithreading),是指从软件或者硬件上实现多个线程并发执行的技术.具有多线程能力的计算机因有硬件支持而能够在同一时间执行多于一个线程,进而提 ...

  5. powerdesigner中把表的描述复制到Name

    '****************************************************************************** '* File: comment2nam ...

  6. oracle分析函数 (转)

    一.总体介绍 12.1 分析函数如何工作 语法 FUNCTION_NAME(<参数>,…) OVER (<PARTITION BY 表达式,…> <ORDER BY 表达 ...

  7. 「LibreOJ β Round #4」游戏

    https://loj.ac/problem/524 题目描述 qmqmqm和sublinekelzrip要进行一场游戏,其规则是这样的: 首先有一个序列,其中每个位置是一个整数或是X.双方轮流将X的 ...

  8. Codeforces 797 D. Broken BST

    D. Broken BST http://codeforces.com/problemset/problem/797/D time limit per test 1 second memory lim ...

  9. Python学习笔记(十五)用Python获取本地数据

    f1 = open(r'E:\Python\Data\data1.txt') #读取data1.txt文件,使用系统默认缓冲区大小, 为了读取快点,使用缓存吧! f = open(r'E:\Pytho ...

  10. Spring Boot 使用IntelliJ IDEA创建一个web开发实例(一)

    .新建项目File-->New-->Project-->Spring Initializr 点击Finish,一个Spring Boot web应用就创建好了.