PHP Filter String Function Example

Source: Internet
Author: User
  1. Class Request

  2. {
  3. Public Function __construct ()
  4. {
  5. if (!GET_MAGIC_QUOTES_GPC ())
  6. {
  7. if (!empty ($_post))
  8. {
  9. foreach ($_post as $k = & $v)
  10. {
  11. if (Is_array ($v))
  12. {
  13. @array_walk ($v, ' urldecode ');
  14. @array_walk ($v, ' addslashes ');
  15. }
  16. Else
  17. {
  18. $v = Addslashes (UrlDecode ($v));
  19. }
  20. $p [$k] = $v;
  21. }
  22. $_post = $p;
  23. Unset ($p);
  24. }
  25. if (!empty ($_get))
  26. {
  27. foreach ($_get as $k = & $v)
  28. {
  29. if (Is_array ($v))
  30. {
  31. @array_walk ($v, ' urldecode ');
  32. @array_walk ($v, ' addslashes ');
  33. }
  34. Else
  35. {
  36. $v = Addslashes (UrlDecode ($v));
  37. }
  38. $g [$k] = $v;
  39. }
  40. $_get = $g;
  41. Unset ($G);
  42. }
  43. }
  44. }
  45. public static function Getquery ($key)
  46. {
  47. if (Isset ($_get[$key]))
  48. {
  49. Return Self::xss_clean ($_get[$key]);
  50. }
  51. Else
  52. {
  53. return false;
  54. }
  55. }
  56. public static function Getpost ($key)
  57. {
  58. if (Isset ($_post[$key]))
  59. {
  60. Return Self::xss_clean ($_post[$key]);
  61. }
  62. Else
  63. {
  64. return false;
  65. }
  66. }
  67. public static function Getserver ($key)
  68. {
  69. $key = Strtoupper ($key);
  70. if (Isset ($_server[$key]))
  71. {
  72. Return Self::xss_clean ($_server[$key]);
  73. }
  74. return false;
  75. }
  76. public static function GetSession ($key)
  77. {
  78. if (Isset ($_session[$key]))
  79. {
  80. Return Self::xss_clean ($_session[$key]);
  81. }
  82. Else
  83. {
  84. return false;
  85. }
  86. }
  87. public static function GetCookie ($key)
  88. {
  89. if (Isset ($_cookie[$key]))
  90. {
  91. return $_cookie[$key];
  92. }
  93. Else
  94. {
  95. return false;
  96. }
  97. }
  98. /**
  99. * Filter illegal characters (distribute)
  100. */
  101. private static function Xss_clean ($STR) {
  102. if (Is_array ($STR) &&!empty ($str)) {
  103. $str = Self::xss_clean_arr ($STR);
  104. } else {
  105. $str = Self::xss_clean_str ($STR);
  106. }
  107. return $str;
  108. }

  109. /**

  110. * Filter illegal characters (arrays)
  111. */
  112. private static function Xss_clean_arr ($STR) {
  113. foreach ($str as $key = = $val) {
  114. if (Is_array ($val)) {
  115. $val = Self::xss_clean_arr ($val);
  116. } else {
  117. $val = Self::xss_clean_str ($val);
  118. }
  119. $arr [$key] = $val;
  120. }
  121. return $arr;
  122. }

  123. /**

  124. * Filter Illegal characters (string)
  125. */
  126. private static function Xss_clean_str ($STR) {
  127. $str = Preg_replace (' # (alert|cmd|passthru|eval|exec|expression|system|fopen|fsockopen|file|file_get_contents| Readfile|unlink) (\s*) \ ((. *?) \) #si ', "\\1\\2 (\\3)", $str);
  128. if (GET_MAGIC_QUOTES_GPC ()) {
  129. return $str;
  130. } else {
  131. Return addslashes ($STR);
  132. }
  133. }
  134. }

Copy Code
  • 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.