Code example for preventing SQL injection by php

Source: Internet
Author: User
Code example for preventing SQL injection by php

  1. Function inject_check ($ SQL _str ){
  2. Return eregi ('select | insert | and | or | update | delete | \ '| \/\ * | \. \. \/| \. \/| union | into | load_file | outfile ', $ SQL _str );
  3. }
  4. If (inject_check ($ _ SERVER ['query _ string']) = 1 or inject_check (file_get_contents ("php: // input") = 1 ){
  5. // Echo "warns of illegal access! ";
  6. Header ("Location: Error. php ");
  7. }

Example 2: Batch filter post and get sensitive data

  1. $ _ GET = stripslashes_array ($ _ GET );

  2. $ _ POST = stripslashes_array ($ _ POST );

  3. // Data filtering function

  4. Function stripslashes_array (& $ array ){
  5. While (list ($ key, $ var) = each ($ array )){
  6. If ($ key! = 'Argc '& $ key! = 'Argv' & (strtoupper ($ key )! = $ Key | ''. intval ($ key) =" $ key ")){
  7. If (is_string ($ var )){
  8. $ Array [$ key] = stripslashes ($ var );
  9. } // Bbs.it-home.org
  10. If (is_array ($ var )){
  11. $ Array [$ key] = stripslashes_array ($ var );
  12. }
  13. }
  14. }
  15. Return $ array;
  16. }

Example 3: replace the HTML tail tag with the filter service.

  1. // Prevents SQL injection
  2. Function lib_replace_end_tag ($ str)
  3. {
  4. If (empty ($ str) return false;
  5. $ Str = htmlspecialchars ($ str );
  6. $ Str = str_replace ('/', "", $ str );
  7. $ Str = str_replace ("\", "", $ str );
  8. $ Str = str_replace (">", "", $ str );
  9. $ Str = str_replace ("<", "", $ str );
  10. $ Str = str_replace ("SCRIPT", "", $ str );
  11. $ Str = str_replace ("SCRIPT", "", $ str );
  12. $ Str = str_replace ("script", "", $ str );
  13. $ Str = str_replace ("script", "", $ str );
  14. $ Str = str_replace ("select", "select", $ str );
  15. $ Str = str_replace ("join", "join", $ str );
  16. $ Str = str_replace ("union", "union", $ str );
  17. $ Str = str_replace ("where", "where", $ str );
  18. $ Str = str_replace ("insert", "insert", $ str );
  19. $ Str = str_replace ("delete", "delete", $ str );
  20. $ Str = str_replace ("update", "update", $ str );
  21. $ Str = str_replace ("like", "like", $ str );
  22. $ Str = str_replace ("drop", "drop", $ str );
  23. $ Str = str_replace ("create", "create", $ str );
  24. $ Str = str_replace ("modify", "modify", $ str );
  25. $ Str = str_replace ("rename", "rename", $ str );
  26. $ Str = str_replace ("alter", "alter", $ str );
  27. $ Str = str_replace ("cas", "cast", $ str );
  28. $ Str = str_replace ("&", "&", $ str );
  29. $ Str = str_replace (">", ">", $ str );
  30. $ Str = str_replace ("<", "<", $ str );
  31. $ Str = str_replace ("", chr (32), $ str );
  32. $ Str = str_replace ("", chr (9), $ str );
  33. $ Str = str_replace ("", chr (9), $ str );
  34. $ Str = str_replace ("&", chr (34), $ str );
  35. $ Str = str_replace ("'", chr (39), $ str );
  36. $ Str = str_replace ("
    ", Chr (13), $ str );
  37. $ Str = str_replace ("'' "," '", $ str );
  38. $ Str = str_replace ("css", "'", $ str );
  39. $ Str = str_replace ("CSS", "'", $ str );
  40. Return $ str;
  41. }

>>> Articles that you may be interested in: sharing php methods to prevent SQL injection and code sharing to prevent SQL injection and cross-site attacks (preliminary practical) php anti-SQL injection function mysql_real_escape_string parsing php prevents SQL injection code collection php prevents SQL injection method parsing php anti-injection code (filter parameters) simple and easy-to-understand php SQL anti-injection code php prevents SQL injection code php implements several methods to prevent SQL injection a good php universal anti-injection program php prevents SQL injection functions

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.