php filter 安全过滤函数
时间:2011-07-02
来源:互联网
参考地址:http://www.php.net/manual/en/book.filter.php
希望大家看得开心,用得放心。
- <?php
- /**
- * @参数验证函数
- * @method:
- * @license http://www.blags.org/
- * @created:2011年07月02日 11时00分
- * @copyright 1997-2011 The Martin Group
- * @author Martin <[email protected]>
- * */
- abstract class CFilter
- {
- /**
- * 类型
- * @var array
- */
- public static $varType = array(
- 'GET' => INPUT_GET,
- 'POST' => INPUT_POST,
- 'COOKIE'=> INPUT_COOKIE,
- 'SERVER'=> INPUT_SERVER,
- 'ENV' => INPUT_ENV
- );
- public static $filterType = array(
- 'STRING' => FILTER_SANITIZE_STRING,
- 'INT' => FILTER_VALIDATE_INT,
- 'BOOLEAN' => FILTER_VALIDATE_BOOLEAN,
- 'FLOAT' => FILTER_VALIDATE_FLOAT,
- 'REGEXP' => FILTER_VALIDATE_REGEXP,
- 'URL' => FILTER_VALIDATE_URL,
- 'EMAIL' => FILTER_VALIDATE_EMAIL,
- 'IP' => FILTER_VALIDATE_IP,
- );
-
- /**
- * 支持过滤列表
- */
- private static function lists()
- {
- return filter_list();
- }
-
- /**
- * 验证类型
- * @param string $type
- */
- public static function filterType($type)
- {
- $filter_list = self::lists();
- return array_search($type,$filter_list) !== false ? true : false;
- }
-
- /**
- *
- * @param $setVarType
- */
- private static function getVarType($setVarType)
- {
- $setVarType = strtoupper($setVarType);
- return isset(self::$varType[$setVarType]) ? self::$varType[$setVarType] : null;
- }
-
- /**
- *
- * @param string $setFilterType
- */
- private static function getFilterType($setFilterType)
- {
- $setFilterType = strtoupper($setFilterType);
- return isset(self::$filterType[$setFilterType]) ? self::$filterType[$setFilterType] : null;
- }
-
- /**
- * 检测参数是否存在
- * @param string $setVarType
- * @param string $varName
- */
- public static function VarExists($setVarType,$varName)
- {
- $FilterVarType = self::getVarType($setVarType);
- if (is_null($FilterVarType))
- return false;
- return filter_has_var(self::$varType[$FilterVarType], $varName);
- }
-
- /**
- *
- * @param string $setVarType
- * @param string $varName
- * @param string $filterType
- */
- public static function FilterInput($setVarType, $varName, $filterType = 'INT')
- {
- $FilterVarType = self::getVarType($setVarType);
- $filterType = self::getFilterType($filterType);
- if (is_null($FilterVarType) || is_null($filterType))
- return false;
- return filter_input($FilterVarType, $varName, $filterType);
- }
-
- /**
- * 验证变量
- * @param string $var
- * @param string $filterType
- */
- public static function FilterVar($var,$filterType)
- {
- $filterType = self::getFilterType($filterType);
- return filter_var($var, $filterType);
- }
-
- /**
- * 字符串
- * @param string $var
- */
- public static function String($var)
- {
- return self::FilterVar($var,'STRING');
- }
-
- public static function Int($var)
- {
- return self::FilterVar($var,'INT');
- }
-
- public static function Boolean($var)
- {
- return self::FilterVar($var,'INT');
- }
-
- public static function Float($var)
- {
- return self::FilterVar($var,'FLOAT');
- }
-
- /**
- *
- * @param string $var
- * @param array $option array("options"=>array("regexp"=>"/^M(.*)/"))
- */
- public static function Regexp($var,$option)
- {
- $filterType = self::getFilterType($filterType);
- return filter_var($var, $filterType, $option);
- }
-
- public static function Url($var)
- {
- return self::FilterVar($var,'URL');
- }
-
- public static function Email($var)
- {
- return self::FilterVar($var,'EMAIL');
- }
-
- public static function Ip($var)
- {
- return self::FilterVar($var,'IP');
- }
-
- }
出处: 马丁博客[http://www.blags.org/]
本文链接地址: http://www.blags.org/php-security-filter-function/
作者: china_code 发布时间: 2011-07-02

作者: china_code 发布时间: 2011-07-02
干嘛用的?
作者: a345868437 发布时间: 2011-07-02
相关阅读 更多
热门阅读
-
office 2019专业增强版最新2021版激活秘钥/序列号/激活码推荐 附激活工具
阅读:74
-
如何安装mysql8.0
阅读:31
-
Word快速设置标题样式步骤详解
阅读:28
-
20+道必知必会的Vue面试题(附答案解析)
阅读:37
-
HTML如何制作表单
阅读:22
-
百词斩可以改天数吗?当然可以,4个步骤轻松修改天数!
阅读:31
-
ET文件格式和XLS格式文件之间如何转化?
阅读:24
-
react和vue的区别及优缺点是什么
阅读:121
-
支付宝人脸识别如何关闭?
阅读:21
-
腾讯微云怎么修改照片或视频备份路径?
阅读:28