PHP uses the filter extension to write parameter-handling classes. http://www.blags.org/php-security-filter-function/ Exp: First introduce the file Include ' filter.php ' $email = cfilter::email ($_post[' email '); Verify that the string is returned successfully, and vice versa returns false.
- /**
- * @ Parameter Validation 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,
- ' Cookies ' = 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,
- );
- /**
- * Filter List Support
- */
- private static function lists ()
- {
- return Filter_list ();
- }
- /**
- * Authentication 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;
- }
- /**
- * Detection parameters are present
- * @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 Variables
- * @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 |