FuelPHP 系列(五) ------ Security 防御

项目中难免会有 form 提交,对用户输入的所有信息进行过滤,可以避免 XSS 攻击,防止 SQL 注入。

一、设置配置信息

首先在 config.php 文件中,对 security 相关信息进行设置,

二、常用方法

1、clean($value, $filters = null)

//将 $text 通过过滤器 filters 进行过滤
$text = "<script>alert(111);</script>";
$filters = array('strip_tags', 'htmlentities', '\\cleaners\\soap::clean');
$text = Security::clean($text, $filters);


//输出结果如下:
string(7) "t(111);" 

2、strip_tags($value) 去除 HTML、PHP 标签

//去除 $text 字符串中的 p 标签
$text = '<p>Test paragraph.</p>';
$text = Security::strip_tags($text);

//输出结果如下:
string(15) "Test paragraph." 

3、xss_clean($value, array $options = array())

//去除 $text 中的标签,保留 <br/>
$text = '<script>alert("XSS attack!")<br/></script>';
$text = Security::xss_clean($text, array('br'));


//输出结果为:
string(39) "alert("XSS attack!")
" 

4、htmlentities($value, $flags = null, $encoding = null, $double_encode = null)

 //和 php 同名函数效果相同
$text = '<p>Test paragraph.</p>';
$text = Security::htmlentities($text);

5、e($string)

  e 函数是 Security::htmlentities. 函数的别名,效果相同

三、在模板中的用法

 

posted @   菜乌  阅读(240)  评论(0编辑  收藏  举报
编辑推荐:
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· C#/.NET/.NET Core技术前沿周刊 | 第 29 期(2025年3.1-3.9)
· 从HTTP原因短语缺失研究HTTP/2和HTTP/3的设计差异
点击右上角即可分享
微信分享提示