Home >Backend Development >PHP Tutorial >php filter security filter function

php filter security filter function

巴扎黑
巴扎黑Original
2016-11-24 14:55:271015browse

php uses parameter processing static classes written by filter extension, welcome to use.
I hope everyone enjoys it and feels comfortable using it.

<?php
/**
 * @参数验证函数
 * @method:
 * @license http://www.blags.org/
 * @created:2011年07月02日 11时00分
 * @copyright 1997-2011 The Martin Group
 * @author Martin <martin@blags.org>
 * */
abstract class CFilter
{
/**
* 类型
* @var array
*/
public static $varType = array(
&#39;GET&#39;=> INPUT_GET,
&#39;POST&#39;=> INPUT_POST,
&#39;COOKIE&#39;=> INPUT_COOKIE,
&#39;SERVER&#39;=> INPUT_SERVER,
&#39;ENV&#39;=> INPUT_ENV
);
public static $filterType = array(
&#39;STRING&#39;=>FILTER_SANITIZE_STRING,
&#39;INT&#39;=>FILTER_VALIDATE_INT,
&#39;BOOLEAN&#39;=>FILTER_VALIDATE_BOOLEAN,
&#39;FLOAT&#39;=>FILTER_VALIDATE_FLOAT,
&#39;REGEXP&#39;=>FILTER_VALIDATE_REGEXP,
&#39;URL&#39;=>FILTER_VALIDATE_URL,
&#39;EMAIL&#39;=>FILTER_VALIDATE_EMAIL,
&#39;IP&#39;=>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 = &#39;INT&#39;)
{
$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,&#39;STRING&#39;);
}
 
public static function Int($var)
{
return self::FilterVar($var,&#39;INT&#39;);
}
 
public static function Boolean($var)
{
return self::FilterVar($var,&#39;INT&#39;);
}
 
public static function Float($var)
{
return self::FilterVar($var,&#39;FLOAT&#39;);
}
 
/**
*
* @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,&#39;URL&#39;);
}
 
public static function Email($var)
{
return self::FilterVar($var,&#39;EMAIL&#39;);
}
 
public static function Ip($var)
{
return self::FilterVar($var,&#39;IP&#39;);
}
 
}


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn