php 字符过滤类,用于过滤各类用户输入的数据
php安全中很重要的就是不要信任用户输入的任何数据,字符串安全过滤就尤为重要,因此提供字符串安全过滤类给大家,以便应对各种安全问题。 折叠 展开 PHP Code 复制内容到剪贴板
php安全中很重要的就是不要信任用户输入的任何数据,字符串安全过滤就尤为重要,因此提供字符串安全过滤类给大家,以便应对各种安全问题。
折叠展开PHP Code复制内容到剪贴板
- <?php
- abstract class Filter { //filter parent class
- private $blackstr = array();
- private $whitestr = array();
- function filtit($str) {
- //do something
- }
- }
- class LoginFilter extends Filter { //for user login filte username(过滤注册的用户名)
- function filtit($str) {
- $this -> blackstr = array(
- ´/[x7f-xff]/´, //filter chinese include chinese symbol
- ´/W/´ //filter all english symbol
- );
- return preg_replace($this->blackstr, ´´, $str);
- }
- }
- class EditorFilter extends Filter { //for article editor filter(过滤在线编辑器内容)
- function filtit($str) {
- $this -> blackstr = array(
- ´/&/´,
- ´/´/´,
- ´/"/´,
- ´/</´,
- ´/>/´,
- ´/\\/´,
- ´///´,
- ´/-/´,
- ´/*/´,
- ´/ /´
- );
- $this -> whitestr = array(
- ´&´,
- ´'´,
- ´"´,
- ´<´,
- ´>´,
- ´´,
- ´/´,
- ´-´,
- ´*´,
- ´ ´
- );
- return preg_replace($this->blackstr, $this -> whitestr, $str);
- }
- }
- class SQLFilter extends Filter { //for filte sql query string(过滤如查询或其它sql语句)
- function filtit($str) {
- $this -> blackstr = array(
- ´/´/´,
- ´/-/´
- );
- return preg_replace($this->blackstr, ´´, $str);
- }
- }
- class FileNameFilter extends Filter { //for filte a file name(过滤文件名如下载文件名)
- function filtit($str) {
- $this -> blackstr = array(
- ´/[^A-za-z0-9_.]|\\|^|[|]/´
- );
- return preg_replace($this->blackstr, ´´, $str);
- }
- }
- ?>
具体应用实例
PHP Code复制内容到剪贴板
- $filter = new FileNameFilter(); //定义实例
- $downFile = $filter->filtit($_GET[´fn´]); //调用过滤方法
- 上一篇:.htaccess用法详解
- 下一篇:用php过滤危险html代码的函数
精彩图集
精彩文章