Home > Backend Development > PHP Tutorial > PHP filter string function example

PHP filter string function example

WBOY
Release: 2016-07-25 08:53:35
Original
1093 people have browsed it
  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 (distribution)
  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 (array)
  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. }

复制代码


Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template