Heim  >  Artikel  >  Backend-Entwicklung  >  php过滤字符串函数示例

php过滤字符串函数示例

WBOY
WBOYOriginal
2016-07-25 08:53:351043Durchsuche
  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. * 过滤非法字符(分发)
  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. * 过滤非法字符(数组)
  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. * 过滤非法字符(字符串)
  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. }
复制代码


Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Vorheriger Artikel:Js php多项选择删除 Nächster Artikel:php打印水仙花数函数代码