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

项目中不免会有 form 提交,对用户输入的全部信息进行过滤,能够避免 XSS 攻击,防止 SQL 注入。php

1、设置配置信息html

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

2、经常使用方法函数

一、clean($value, $filters = null)spa

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


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

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

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

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

三、xss_clean($value, array $options = array())orm

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


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

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

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

五、e($string)blog

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

3、在模板中的用法

相关文章
相关标签/搜索