PHP filter Special characters SQL anti-injection code

Source: Internet
Author: User
Tags php form php form submit
  1. Method One
  2. Filter ', ', SQL name
  3. Addslashes ();
  4. Method Two, remove all HTML tags
  5. Strip_tags ();
  6. Method Three filtering may produce code
  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 unsafe labels directly, you can leave this 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. ?>
Copy Code

How to use: Copy the above code to create a new sqlin.php file, and then include the page with Get or post data received

Principle Analysis: Replace all SQL keywords with empty This code can not be used in the message book, in order to use in the message book please replace the ... $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. ?>
Copy Code

-------------------------------------------------------addslashes--using a backslash to reference a string

String addslashes (String str)

Returns a string that is preceded by a backslash in order for the database query statement to be preceded by some characters. These characters are single quotes ('), double quotation marks ("), backslashes (\), and NUL (the NULL character).

An example of using addslashes () is when you want to enter data into the database. For example, the name O ' Reilly is inserted into the database, which needs to be escaped. Most databases use \ as escape character: O\ ' Reilly. This allows the data to be placed in the database without inserting additional \. When PHP instruction Magic_quotes_sybase is set to ON, it means that the insert ' will be used ' to escape.

By default, PHP instruction MAGIC_QUOTES_GPC is on, and it is primarily for all GET, POST, and COOKIE data to run automatically addslashes (). Do not use Addslashes () for strings that have been MAGIC_QUOTES_GPC escaped, because this results in double-layer escaping. You can use the function GET_MAGIC_QUOTES_GPC () to detect this situation. GET_MAGIC_QUOTES_GPC () This function gets the variable MAGIC_QUOTES_GPC (GPC, Get/post/cookie) value of the PHP environment configuration. Returns 0 to turn off this function; return 1 indicates that this function is turned on. When MAGIC_QUOTES_GPC is turned on, all the ' (single quotes), ' (double quotes), \ (backslash) and null characters are automatically converted to the overflow character that contains the backslash. Addslashes and Stripslashes are the characters in PHP that operate on the database. At first glance it seems difficult to remember, but as long as the analysis, add is increased, strip is ignored. Slash is a slash, Slash is the plural of the slash. Then addslashes is to increase the meaning of the slash, because some special characters are written to the database after the problem, such as "', so add \ to the special symbol escape, tell the database those special symbols are strings, Similarly stripslashes is to subtract a slash when extracting a string from a database. Htmlspecialchars some special characters into the HTML encoding, commonly used in the occasion may be to deal with the message of the customer message version. These special characters are limited to the functions of the following:& & <, <>, >htmlentities and Htmlspecialchars. However, Htmlentities is not spared all the HTML-defined entity, including various special characters and Chinese, so that the result is that the Chinese character part becomes a bunch of garbled. Htmlspecialchars_decode is the reverse process of htmlspecialchars, which converts HTML encoding into characters. PHP Filter Special characters utility function php form Submit special character filter method HTML special character filter PHP class URL link special character escape method php special character escape detailed PHP filter parameters special characters anti-injection PHP Methods for filtering illegal and special strings PHP special character handling function example

  • Related Article

    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.