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. Linux基础-正则表达式整理---------------grep、sed、awk

    目录:    Ⅰ:正则表达式    Ⅱ:作业 Ⅰ:正则表达式 正则就是用一些具有特殊含义的符号组合到一起(称为正则表达式)来描述字符或者字符串的方法.或者说:正则就是用来描述一类事物的规则. 在lin ...

  2. 【字符串】KMP字符串匹配

    百度百科 Definition \(KMP\)算法是一个字符串匹配算法.他接收两个字符串\(A,B\),返回\(B\)在\(A\)中出现的所有位置. 以下称需要被匹配的串\(A\)为主串,可能在主串中 ...

  3. 生存分析/Weibull Distribution韦布尔分布

    sklearn实战-乳腺癌细胞数据挖掘(博主亲自录制视频教程) https://study.163.com/course/introduction.htm?courseId=1005269003&am ...

  4. Centos7系统环境下Solr之Java实战(二)制定中文分析器、配置业务域

    制定中文分析器 1.把IKAnalyzer2012FF_u1.jar添加到solr工程的lib目录下 2.把扩展词典.配置文件放到solr工程的WEB-INF/classes目录下. 配置一个Fiel ...

  5. 【BZOJ】4766: 文艺计算姬

    [题目]给定两边节点数为n和m的完全二分图,求生成树数取模给定的p.n,m,p<=10^18. [算法]生成树计数(矩阵树定理) [题解]参考自 [bzoj4766]文艺计算姬 by WerKe ...

  6. 【leetcode 简单】第二十二题 对称二叉树

    给定一个二叉树,检查它是否是镜像对称的. 例如,二叉树 [1,2,2,3,4,4,3] 是对称的. 1 / \ 2 2 / \ / \ 3 4 4 3 但是下面这个 [1,2,2,null,3,nul ...

  7. HDU 1059 Dividing (dp)

    题目链接 Problem Description Marsha and Bill own a collection of marbles. They want to split the collect ...

  8. 【译】第二篇 SQL Server代理作业步骤和子系统

    本篇文章是SQL Server代理系列的第二篇,详细内容请参考原文. SQL Server代理作业由一系列的一个或多个作业步骤组成.一个作业步骤分配给一个特定的作业子系统(确定作业步骤去完成的工作). ...

  9. Vue SPA 首屏加载优化实践

    写在前面 本文记录笔者在Vue SPA项目首屏加载优化过程中遇到的一些坑及优化方案! 我们以 vue-cli 工具为例,使用 vue-router 搭建SPA应用,UI框架选用 element-ui ...

  10. Java的继承和多态

    看了博客园里面的一个文章,关于java的继承和多态: class A ...{ public String show(D obj)...{ return ("A and D"); ...