Good PHP code to prevent SQL Injection Vulnerability filter functions

Source: Internet
Author: User
  1. PHP Whole station Anti-injection program, need to require_once this file in public file
  2. Judging MAGIC_QUOTES_GPC Status
  3. if (@get_magic_quotes_gpc ()) {
  4. $_get = sec ($_get);
  5. $_post = sec ($_post);
  6. $_cookie = sec ($_cookie);
  7. $_files = sec ($_files);
  8. }
  9. $_server = sec ($_server);
  10. Function sec (& $array) {
  11. If it is an array, iterate through the array, calling recursively
  12. if (Is_array ($array)) {
  13. foreach ($array as $k = = $v) {
  14. $array [$k] = sec ($v);
  15. }
  16. } else if (is_string ($array)) {
  17. Use the Addslashes function to handle
  18. $array = Addslashes ($array);
  19. } else if (Is_numeric ($array)) {
  20. $array = Intval ($array);
  21. }
  22. return $array;
  23. }
  24. Integer Filter function
  25. function Num_check ($id) {
  26. if (! $id) {
  27. Die (' parameter cannot be empty! ' );
  28. }//Is null-judged
  29. else if (Inject_check ($id)) {
  30. Die (' illegal parameters ');
  31. }//Injection judgment
  32. else if (! is_numetic ($id)) {
  33. Die (' illegal parameters ');
  34. }
  35. Digital judgment
  36. $id = Intval ($id);
  37. The whole type of
  38. return $id;
  39. }
  40. Character Filter function
  41. function Str_check ($STR) {
  42. if (Inject_check ($STR)) {
  43. Die (' illegal parameters ');
  44. }
  45. Injection judgment
  46. $str = Htmlspecialchars ($STR);
  47. Convert HTML
  48. return $str;
  49. }
  50. function Search_check ($STR) {
  51. $str = Str_replace ("_", "\_", $str);
  52. Filter Out "_"
  53. $str = str_replace ("%", "\%", $str);
  54. Filter out "%"
  55. $str = Htmlspecialchars ($STR);
  56. Convert HTML
  57. return $str;
  58. }
  59. Form Filter function
  60. function Post_check ($str, $min, $max) {
  61. if (Isset ($min) && strlen ($STR) < $min) {
  62. Die (' minimum $min bytes ');
  63. } else if (Isset ($max) && strlen ($STR) > $max) {
  64. Die (' Up to $max bytes ');
  65. }
  66. Return Stripslashes_array ($STR);
  67. }
  68. Anti-injection function
  69. function Inject_check ($sql _str) {
  70. Return eregi (' select|inert|update|delete|\ ' |\/\*|\*|\.\.\/|\.\/| Union|into|load_file|outfile ', $sql _str);
  71. Filtration, anti-injection
  72. }
  73. Function Stripslashes_array (& $array) {
  74. if (Is_array ($array)) {
  75. foreach ($array as $k = = $v) {
  76. $array [$k] = Stripslashes_array ($v);
  77. }
  78. } else if (is_string ($array)) {
  79. $array = Stripslashes ($array);
  80. }
  81. return $array;
  82. }
  83. ?>
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.