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. Zookeeper(四)Hadoop HA高可用集群搭建

    一.高可就集群搭建 1.集群规划 2.集群服务器准备 (1) 修改主机名(2) 修改 IP 地址(3) 添加主机名和 IP 映射(4) 同步服务器时间(5) 关闭防火墙(6) 配置免密登录(7) 安装 ...

  2. [学习笔记]Min-25筛

    %%yyb %%zsy 一. 基本操作:筛1~N中的素数个数.n=1e9 设F(M,j)表示,2~M的所有数中,满足以下条件之一的数的个数:①x是质数②x最小质因子大于(注意是大于没有等号)$P_j$ ...

  3. [学习笔记]矩阵乘法及其优化dp

    1.定义: $c[i][j]=\sum a[i][k]\times b[k][j]$ 所以矩阵乘法有条件,(n*m)*(m*p)=n*p 即第一个矩阵的列数等于第二个矩阵的行数,否则没有意义. 2.结 ...

  4. 【BZOJ 1129】[POI2008]Per 二叉堆

    这个东西读完题之后,就能知道我们要逐位计算贡献.推一下式子,会发现,这一位的贡献,是当前剩余的数字形成的序列的总数,乘上所剩数字中小于s上这一位的数的个数与所剩数字的总数的比.所以我们维护“当前剩余的 ...

  5. socket编程学习step2

    引言:主机之间如何相互交互呢?网络层的“ip地址”可以唯一标识网络中的主机,而传输层的“协议+端口“可以唯一标识主机中的应用进程.这样利用三元组(ip地址,协议,端口)就可以标识网络的进程了,网络中的 ...

  6. windows环境libevent搭建和demo分析

    libevent框架之前有做过分析,这次是谈谈如何将libevent搭建在vs工作环境下, 并且编写一个demo进行测试.测试过程中会再一次带大家分析消息是怎么传递 的. 我的libevent版本li ...

  7. Redis 为什么用跳表而不用平衡树

    Redis 为什么用跳表而不用平衡树? 本文是<Redis内部数据结构详解>系列的第六篇.在本文中,我们围绕一个Redis的内部数据结构--skiplist展开讨论. Redis里面使用s ...

  8. centos中设置swap交换空间的大小设置和swappiness的比例设置

    首先使用free -m命令查看内存使用情况和swap的大小 关闭swap: 设置swap的大小: bs指的是Block Size,就是每一块的大小.这里的例子是1M,意思就是count的数字,是以1M ...

  9. Bootstrap 按钮组

    连在一起的按钮:.btn-group <div class="btn-group" role="group"> <button class=& ...

  10. Codeforces 450B div.2 Jzzhu and Sequences 矩阵快速幂or规律

    Jzzhu has invented a kind of sequences, they meet the following property: You are given x and y, ple ...