**CodeIgniter系列 添加filter和helper
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的更多相关文章
- [Asp.net MVC]Asp.net MVC5系列——添加模型
目录 概述 添加模型 总结 系列文章 [Asp.net MVC]Asp.net MVC5系列——第一个项目 [Asp.net MVC]Asp.net MVC5系列——添加视图 概述 在本节中我们将追加 ...
- [Asp.net MVC]Asp.net MVC5系列——添加数据
目录 概述 显示添加数据时所用表单 处理HTTP-POST 总结 系列文章 [Asp.net MVC]Asp.net MVC5系列——第一个项目 [Asp.net MVC]Asp.net MVC5系列 ...
- Asp.net MVC3 中,动态添加filter
Asp.net MVC3 中,动态添加filter filter是attribute,不支持泛型,传入的参数必须是固定的值.总之很受attribute本身的限制. 发现一篇老外的文章,动态设置filt ...
- 通过添加filter过滤器 彻底解决ajax 跨域问题
1.在web.xml添加filter <filter> <filter-name>contextfilter</filter-name> <filter-cl ...
- Jetty添加Filter过滤器
1.Jetty嵌入到Spring项目 try { Server server = new Server(8080); WebAppContext context = new WebAppContext ...
- java项目中通过添加filter过滤器解决ajax跨域问题
1.在web.xml添加filter <filter> <filter-name>contextfilter</filter-name> <filter-cl ...
- Springboot添加filter方法
在springboot添加filter有两种方式: (1).通过创建FilterRegistrationBean的方式(建议使用此种方式,统一管理,且通过注解的方式若不是本地调试,如果在filter中 ...
- [Asp.net MVC]Asp.net MVC5系列——添加视图
目录 系列文章 概述 添加视图 总结 系列文章 [Asp.net MVC]Asp.net MVC5系列——第一个项目 概述 在这一部分我们添加一个新的控制器HelloWorldController类, ...
- cas sso单点登录系列1_cas-client Filter源码解码(转)
转:http://blog.csdn.net/ae6623/article/details/8841801?utm_source=tuicool&utm_medium=referral /* ...
随机推荐
- python编码问题FAQ
http://note.youdao.com/noteshare?id=2cfb0ac4da042c2550aa3918beda81ec
- go defer注意点,很容易出错的!!!
1:defer是在return之前执行的 函数返回的过程是这样的:先给返回值赋值,然后调用defer表达式,最后才是返回到调用函数中 返回值 = xxx 调用defer函数 空的return fun ...
- Python/spss-多元回归建模-共线性诊断2(推荐AA)
sklearn实战-乳腺癌细胞数据挖掘(博客主亲自录制视频教程) https://study.163.com/course/introduction.htm?courseId=1005269003&a ...
- js知识点乱炖
修改属性 元素.style.样式=值 document.getElementById('box').style.width='200px'; 属性操作方式 1.. 的 元素.属性名如果属性是单 ...
- Java学习遇到的问题
一. Java中泛型如何比较大小,继承Comparable类,然后实现其唯一的方法compareTo(): public class GenericClass<E extends Compara ...
- maven使用过程中遇到的问题总汇
1:web.xml is missing and <failOnMissingWebXml> is set to true 造成原因: 使用maven创建项目时有时在pom.xml的war ...
- Shodan 使用
本文来自:Shodan新手入坑指南, 记录简要用法,以便使用. 文章先给出搜索过滤方法,然后再简单介绍两种使用shodan的方法:使用命令和编写代码. 搜索过滤 hostname:搜索指定的主机或域名 ...
- Chrome浏览器任意修改网页内容
在Chrome浏览器按F12,打开开发者工具,切换到console选项卡: 在下面的输入行输入下面的命令回车: document.body.contentEditable="true&quo ...
- Django(基础篇)
1.请求周期 url> 路由 > 函数或类 > 返回字符串或者模板语言? Form表单提交: 提交 -> url > 函数或类中的方法 ...
- PHP深浅拷贝
举个栗子: <?php class Example1 { public $name; public function __construct($name) { $this->name = ...