PHP SQL anti-injection

Source: Internet
Author: User
Tags php and mysql

There are not too many filters, mainly for PHP and MySQL combination.
General anti-injection, as long as the use of PHP addslashes function is possible.

Here's a copy of the code:

PHP Code
    1. $_post = Sql_injection ($_post);
    2. $_get = Sql_injection ($_get);
    3. function Sql_injection ($content)
    4. {
    5. if (!GET_MAGIC_QUOTES_GPC ()) {
    6. if (Is_array ($content)) {
    7. foreach ($content as $key = = $value) {
    8. $content [$key] = addslashes ($value);
    9. }
    10. } else {
    11. Addslashes ($content);
    12. }
    13. }
    14. return $content;
    15. }

To do the system, you can use the following code, but also copy.

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

PHP SQL anti-injection

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.