Php filters out special characters of SQL anti-injection code

Source: Internet
Author: User
Tags php form
Php filters out special characters of SQL anti-injection code

  1. // Method 1
  2. // Filter ', ", SQL language name
  3. Addslashes ();
  4. // Method 2: Remove all html tags
  5. Strip_tags ();
  6. // Method 3: filter the code that may be generated
  7. Function php_sava ($ str)
  8. {
  9. $ Farr = array (
  10. "/S + /",
  11. "/<(/?) (Script | I? Frame | style | html | body | title | link | meta |? | %) ([^>] *?)> /IsU ",
  12. "/(<[^>] *) On [a-zA-Z] + s * = ([^>] *>)/isU ",
  13. );
  14. $ Tarr = array (
  15. "",
  16. "<>", // If you want to clear insecure labels directly, leave it blank.
  17. "",
  18. );
  19. $ Str = preg_replace ($ farr, $ tarr, $ str );
  20. Return $ str;
  21. }
  22. // Php SQL anti-injection code
  23. Class sqlin
  24. {
  25. // Dowith_ SQL ($ value)
  26. Function dowith_ SQL ($ str)
  27. {
  28. $ Str = str_replace ("and", "", $ str );
  29. $ Str = str_replace ("execute", "", $ str );
  30. $ Str = str_replace ("update", "", $ str );
  31. $ Str = str_replace ("count", "", $ str );
  32. $ Str = str_replace ("chr", "", $ str );
  33. $ Str = str_replace ("mid", "", $ str );
  34. $ Str = str_replace ("master", "", $ str );
  35. $ Str = str_replace ("truncate", "", $ str );
  36. $ Str = str_replace ("char", "", $ str );
  37. $ Str = str_replace ("declare", "", $ str );
  38. $ Str = str_replace ("select", "", $ str );
  39. $ Str = str_replace ("create", "", $ str );
  40. $ Str = str_replace ("delete", "", $ str );
  41. $ Str = str_replace ("insert", "", $ str );
  42. $ Str = str_replace ("'", "", $ str );
  43. $ Str = str_replace ("", "", $ str );
  44. $ Str = str_replace ("", "", $ str );
  45. $ Str = str_replace ("or", "", $ str );
  46. $ Str = str_replace ("=", "", $ str );
  47. $ Str = str_replace ("% 20", "", $ str );
  48. // Echo $ str;
  49. Return $ str;
  50. }
  51. // Aticle () anti-SQL injection function // php Tutorial
  52. Function sqlin ()
  53. {
  54. Foreach ($ _ GET as $ key => $ value)
  55. {
  56. $ _ GET [$ key] = $ this-> dowith_ SQL ($ value );
  57. }
  58. Foreach ($ _ POST as $ key => $ value)
  59. {
  60. $ _ POST [$ key] = $ this-> dowith_ SQL ($ value );
  61. }
  62. }
  63. }
  64. $ Dbsql = new sqlin ();
  65. ?>

Usage: copy the above code to create a new sqlin. php file, and then include the page for receiving GET or POST data.

Principle analysis: Replace all SQL keywords with NULL. this code cannot be used in the message book. to use it in the message book, replace ....... $ str = str_replace ("and", "", $ str); to $ str = str_replace ("% 20", "", $ str );... the code is:

  1. $ Str = str_replace ("and", "and", $ str );
  2. $ Str = str_replace ("execute", "execute", $ str );
  3. $ Str = str_replace ("update", "update", $ str );
  4. $ Str = str_replace ("count", "count", $ str );
  5. $ Str = str_replace ("chr", "chr", $ str );
  6. $ Str = str_replace ("mid", "mid", $ str );
  7. $ Str = str_replace ("master", "master", $ str );
  8. $ Str = str_replace ("truncate", "truncate", $ str );
  9. $ Str = str_replace ("char", "char", $ str );
  10. $ Str = str_replace ("declare", "declare", $ str );
  11. $ Str = str_replace ("select", "select", $ str );
  12. $ Str = str_replace ("create", "create", $ str );
  13. $ Str = str_replace ("delete", "delete", $ str );
  14. $ Str = str_replace ("insert", "insert", $ str );
  15. $ Str = str_replace ("'", "'", $ str );
  16. $ Str = str_replace ("", ", $ str );
  17. ?>

------------------------------------------------------- Addslashes -- use a backslash to reference a string

String addslashes (string str)

Returns a string that requires a backslash before certain characters for database query statements. These characters are single quotation marks ('), double quotation marks ("), backslash (\), and NUL (NULL ).

An example of using addslashes () is when you want to input data into the database. For example, insert the name 'Reilly into the database, which requires escaping. Most databases use \ as the escape character: O \ 'Reilly. In this way, the data can be put into the database without inserting additional \. When the PHP command magic_quotes_sybase is set to on, it means that when 'is inserted,' is used for escape.

By default, the PHP command magic_quotes_gpc is on, which automatically runs addslashes () on all GET, POST, and COOKIE data (). Do not use addslashes () for strings that have been escaped by magic_quotes_gpc, because this causes double-layer escape. In this case, you can use the get_magic_quotes_gpc () function for detection. Get_magic_quotes_gpc () this function obtains the magic_quotes_gpc (GPC, Get/Post/Cookie) value of the variable configured in the PHP environment. If 0 is returned, this function is disabled. if 1 is returned, this function is enabled. When magic_quotes_gpc is enabled, all '(single quotation marks),' (double quotation marks), \ (backslash) and null characters are automatically converted to overflow characters containing the backslash. When addslashes and stripslashes are used to operate database characters in php. at first glance, it seems hard to remember, but as long as you analyze it, add is added, strip is ignored. slash is the diagonal line, and slash is the plural number of the diagonal line. then, addslashes is used to add diagonal lines, because some special characters may cause problems after being written to the database, such as "', so we need to add \ to escape special characters, tell the database that the special symbols are strings. Similarly, when stripslashes extracts strings from the database, it will subtract the diagonal line. htmlspecialchars converts some special characters into html encoding, which may be the message board used to process customers' messages. These special characters are limited to the following: &-> & "->" <-> <>-> htmlentities are similar to htmlspecialchars, however, htmlentities are reserved for all the entity defined in HTML, including various special characters and Chinese characters. as a result, the Chinese characters are changed to a bunch of garbled characters. Htmlspecialchars_decode is the reverse process of htmlspecialchars, which converts html encoding into characters. Php filter special character utility functions php form submission special character filtering methods html special character filtering special character escaping methods in php url links php special character escaping explanation php filter parameters special characters anti-injection php filter an example of php special character processing function for illegal methods with special strings

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.