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. 函数的别名,效果相同

三、在模板中的用法

猜你喜欢

转载自www.cnblogs.com/rendd/p/9291103.html
今日推荐