PHP prevents SQL injection function usage

Source: Internet
Author: User
Tags derick

Use the Addslashes function in PHP to prevent SQL injection.

Although many PHP programmers in the country still rely on addslashes to prevent SQL injection, it is recommended to strengthen Chinese to prevent SQL injection check.

The problem with addslashes is that hackers can use 0xbf27 instead of single quotes, and addslashes just modifies 0xbf27 to 0xbf5c27 as a valid multibyte character, where 0xbf5c is still considered a single quote, So addslashes can't intercept successfully.

Of course addslashes is not useless, it is used for single-byte string processing, multibyte characters or mysql_real_escape_string bar.

Also for the use of the GET_MAGIC_QUOTES_GPC function in the PHP manual, consider the following example.

Example:

  1. function Post_check ($post)
  2. {
  3. if (!GET_MAGIC_QUOTES_GPC ())//Determine if MAGIC_QUOTES_GPC is open
  4. {
  5. $post = Addslashes ($post); To filter the submission data without opening the MAGIC_QUOTES_GPC
  6. }
  7. $post = Str_replace ("_", "_", $post); Filter out the ' _ '
  8. $post = str_replace ("%", "%", $post); Filter out the '% '
  9. $post = NL2BR ($post); Carriage return Conversion
  10. $post = Htmlspecialchars ($post); HTML markup Conversions
  11. return $post;
  12. }
  13. ?>
  14. Or
  15. function Inject_check ($sql _str)
  16. {
  17. Return eregi (' select|insert|update|delete| ' |
  18. function verify_id ($id =null)
  19. {
  20. if (! $id) {exit (' No arguments are submitted! '); }//Is null-judged
  21. ElseIf (Inject_check ($id)) {exit (' argument submitted is illegal! '); }//Injection judgment
  22. ElseIf (!is_numeric ($id)) {exit (' argument submitted is illegal! '); }//Digital judgment
  23. $id = Intval ($id); The whole type of
  24. return $id;
  25. }
  26. ?>
Copy Code

String mysql_real_escape_string (String $unescaped _string [, Resource $link _identifier])

This function escapes the special characters in unescaped_string and counts the current character set of the connection, so it can be used safely for mysql_query ().

Note:mysql_real_escape_string () does not escape% and _.

Mysql_real_escape_string

Example 1,mysql_real_escape_string ()

    1. $item = "Zak s and Derick ' s Laptop";
    2. $escaped _item = mysql_real_escape_string ($item);
    3. printf ("Escaped string:%sn", $escaped _item);
    4. ?>
Copy Code

Output: Escaped String:zak ' s and Derick ' s Laptop

Mysql_escape_string This function escapes unescaped_string so that it can be used safely for mysql_query (). Note: mysql_escape_string () does not escape% and _. This function is exactly the same as mysql_real_escape_string () except that mysql_real_escape_string () accepts a connection handle and transfers the string in accordance with the current character set. The mysql_escape_string () does not accept the connection parameters, and regardless of the current character set settings.

Example 1. Mysql_escape_string () example

    1. $item = "Zak ' s Laptop";
    2. $escaped _item = mysql_escape_string ($item);
    3. printf ("Escaped string:%sn", $escaped _item);
    4. ?>
Copy Code

Output: Escaped String:zak ' s Laptop

The difference between the 2 functions of mysql_real_escape_string and mysql_escape_string: mysql_real_escape_string must be used in cases (PHP 4 >= 4.3.0, PHP 5). Otherwise you can only use mysql_escape_string, the difference between the two is: Mysql_real_escape_string consider the current character set of the connection, and mysql_escape_string not consider. can use judgment to synthesize the processing.

Example:

    1. function Cleanuserinput ($dirty) {
    2. if (GET_MAGIC_QUOTES_GPC ()) {
    3. $clean = mysql_real_escape_string (stripslashes ($dirty));
    4. }else{
    5. $clean = mysql_real_escape_string ($dirty);
    6. }
    7. return $clean;
    8. }
Copy Code

Summary: * Addslashes () is forcibly added; * Mysql_real_escape_string () will determine the character set, but the PHP version is required; * Mysql_escape_string does not consider the current character set of the connection.

  • 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.