PHP uses the filter extension to write a parameter processing class.
http://www.blags.org/php-security-filter-function/
exp:
First import the file
include 'filter.php'
$email = CFilter::Email($_POST['email']);
If the verification is successful, it will return a string, otherwise it will return false.
- /**
- * @parameter verification function
- * @method:
- * @license http://www.blags.org/
- * @created: July 02, 2011 11:00
- * @copyright 1997-2011 The Martin Group
- * @author Martin
- **/
- abstract class CFilter
- {
- /**
- * type
- * @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_REGEX P,
- 'URL' => FILTER_VALIDATE_URL,
- 'EMAIL' => FILTER_VALIDATE_EMAIL,
- 'IP' => FILTER_VALIDATE_IP,
- );
-
- /**
- * Support filter list
- */
- private static function lists()
- {
- return filter_list();
- }
-
- / **
- * Validation type
- * @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;
- }
-
- /**
- * Check whether the parameter exists
- * @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);
- }
-
- /**
- * Validation variable
- * @param string $var
- * @param string $filterType
- */
- public static function FilterVar($ var,$filterType)
- {
- $filterType = self::getFilterType($filterType);
- return filter_var($var, $filterType);
- }
-
- /**
- * string
- * @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');
- }
-
- }
-
Copy code
|