PHP Filter functions

Source: Internet
Author: User
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.
  1. /**
  2. * @ Parameter Validation function
  3. * @method:
  4. * @license http://www.blags.org/
  5. * @created: July 02, 2011 11:00
  6. * @copyright 1997-2011 the Martin Group
  7. * @author Martin
  8. * */
  9. Abstract class Cfilter
  10. {
  11. /**
  12. * Type
  13. * @var Array
  14. */
  15. public static $varType = Array (
  16. ' GET ' = Input_get,
  17. ' POST ' = Input_post,
  18. ' Cookies ' = Input_cookie,
  19. ' SERVER ' = Input_server,
  20. ' ENV ' = input_env
  21. );
  22. public static $filterType = Array (
  23. ' STRING ' =>filter_sanitize_string,
  24. ' INT ' =>filter_validate_int,
  25. ' BOOLEAN ' =>filter_validate_boolean,
  26. ' FLOAT ' =>filter_validate_float,
  27. ' REGEXP ' =>filter_validate_regexp,
  28. ' URL ' =>filter_validate_url,
  29. ' EMAIL ' =>filter_validate_email,
  30. ' IP ' =>filter_validate_ip,
  31. );
  32. /**
  33. * Filter List Support
  34. */
  35. private static function lists ()
  36. {
  37. return Filter_list ();
  38. }
  39. /**
  40. * Authentication Type
  41. * @param string $type
  42. */
  43. public static function FilterType ($type)
  44. {
  45. $filter _list = self::lists ();
  46. Return Array_search ($type, $filter _list)!== false? True:false;
  47. }
  48. /**
  49. *
  50. * @param $setVarType
  51. */
  52. private static function Getvartype ($setVarType)
  53. {
  54. $setVarType = Strtoupper ($setVarType);
  55. Return Isset (self:: $varType [$setVarType])? Self:: $varType [$setVarType]: null;
  56. }
  57. /**
  58. *
  59. * @param string $setFilterType
  60. */
  61. private static function Getfiltertype ($setFilterType)
  62. {
  63. $setFilterType = Strtoupper ($setFilterType);
  64. Return Isset (self:: $filterType [$setFilterType])? Self:: $filterType [$setFilterType]: null;
  65. }
  66. /**
  67. * Detection parameters are present
  68. * @param string $setVarType
  69. * @param string $varName
  70. */
  71. public static function Varexists ($setVarType, $varName)
  72. {
  73. $FilterVarType = Self::getvartype ($setVarType);
  74. if (Is_null ($FilterVarType))
  75. return false;
  76. Return Filter_has_var (self:: $varType [$FilterVarType], $varName);
  77. }
  78. /**
  79. *
  80. * @param string $setVarType
  81. * @param string $varName
  82. * @param string $filterType
  83. */
  84. public static function Filterinput ($setVarType, $varName, $filterType = ' INT ')
  85. {
  86. $FilterVarType = Self::getvartype ($setVarType);
  87. $filterType = Self::getfiltertype ($filterType);
  88. if (Is_null ($FilterVarType) | | is_null ($FILTERTYPE))
  89. return false;
  90. Return Filter_input ($FilterVarType, $varName, $filterType);
  91. }
  92. /**
  93. * Validation Variables
  94. * @param string $var
  95. * @param string $filterType
  96. */
  97. public static function Filtervar ($var, $filterType)
  98. {
  99. $filterType = Self::getfiltertype ($filterType);
  100. Return Filter_var ($var, $filterType);
  101. }
  102. /**
  103. * String
  104. * @param string $var
  105. */
  106. public static function String ($var)
  107. {
  108. Return Self::filtervar ($var, ' STRING ');
  109. }
  110. public static function Int ($var)
  111. {
  112. Return Self::filtervar ($var, ' INT ');
  113. }
  114. public static function Boolean ($var)
  115. {
  116. Return Self::filtervar ($var, ' INT ');
  117. }
  118. public static function Float ($var)
  119. {
  120. Return Self::filtervar ($var, ' FLOAT ');
  121. }
  122. /**
  123. *
  124. * @param string $var
  125. * @param array $option Array ("Options" =>array ("regexp" = "/^m (. *)/"))
  126. */
  127. public static function Regexp ($var, $option)
  128. {
  129. $filterType = Self::getfiltertype ($filterType);
  130. Return Filter_var ($var, $filterType, $option);
  131. }
  132. public static function Url ($var)
  133. {
  134. Return Self::filtervar ($var, ' URL ');
  135. }
  136. public static function Email ($var)
  137. {
  138. Return Self::filtervar ($var, ' EMAIL ');
  139. }
  140. public static function Ip ($var)
  141. {
  142. Return Self::filtervar ($var, ' IP ');
  143. }
  144. }
Copy Code
  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.