**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快速教程 - 基础篇(转)
原文:https://zhuanlan.zhihu.com/p/24162430 5.1 Python简介 本章将介绍Python的最基本语法,以及一些和深度学习还有计算机视觉最相关的基本使用. 5. ...
- redis 字符串的管理的一点理解
redis字符串可以实现通过地址偏移找到所在结构体的首地址,struct sdshdr *sh = (void *)(s - (sizeof(struct sdshdr))) 也就是通过buf地址可以 ...
- ZABBIX 3.0 配置监控NGINX性能【OK】
1.在agent端查看配置: nginx -V //查看编辑时是否加入状态监控模块:--with-http_stub_status_module --with-http_gzip_static_mod ...
- SSH免密码登录,实现数据传输备份
简单来说,就是通过ssh-keygen -t rsa命令来产生一组公私钥,私钥是id_rsa,公钥是id_rsa.pub,把公钥上传到另一台服务器对应账号的.ssh/authorized_keys,即 ...
- errno错误号含义
errno0 : Success errno1 : Operation not permitted errno2 : No such file or directory errno3 : No suc ...
- Shell编程——vim常用命令
[vim]工作模式切换: 在普通模式下输入 i(插入).c(修改).o(另起一行) 命令时进入编辑模式:按 esc 键退回到普通模式. 在普通模式下输入冒号(:)可以进入命令模式.输入完命 ...
- 从零搭建SSM框架(五)Maven实现Tomcat热部署
配置tomcat 第一步:需要修改tomcat的conf/tomcat-users.xml配置文件.添加用户名.密码.权限. <role rolename="manager-gui&q ...
- 【洛谷 P4219】 [BJOI2014]大融合(LCT)
题目链接 维护子树信息向来不是\(LCT\)所擅长的,所以我没搞懂qwq 权当背背模板吧.Flash巨佬的blog里面写了虽然我没看懂. #include <cstdio> #define ...
- 利用media query写响应式布局
最近才接触到响应式布局的概念,之前用到的bootstrap就是一种响应式布局的框架.学习的时候参考了http://blog.csdn.net/shoyer/article/details/829301 ...
- Sublime删除项目删不掉?
最近用sublime进行项目的开发,感觉懵逼的像个小白菜~~ 今天遇到的问题可是一个超级白痴的问题,sublime的空白项目文件夹怎么都删不掉,我的傻逼操作是:选中文件--->Delete--- ...