Prevent MySQL Injection

Source: Internet
Author: User
Tags mysql injection mysql query simple sql injection

Simple background: SQL injection is a very hot topic in the field of web security. SQL injection is mainly due to splicing SQL statements, the attacker through the passing of illegal parameters to make the stitching of SQL into non-programmer-intended SQL query, the attacker can complete the login, delete user data and even compromise the system security.

Through StackOverflow, you can see a more secure approach to prevention, there are two main ways: Mysqli and PDO. But both of these are processed by pre-defined and parameter bindings , which can almost prevent any SQL injection.

For users who use only the basic MySQL query, there are some ways to prevent simple SQL injection by escaping and filtering. The code is as follows:

<?phppublic function Prevent_injection ($temp) {        //filter keyword        $temp = preg_replace ('/select|and|or|insert| Update|delete/i ', ', $temp);        if (Is_numeric ($temp)) {            //type detection            $temp = intval ($temp);        } else if (!GET_MAGIC_QUOTES_GPC ()) {             //escape             $temp = addslashes ($temp);        }        return $temp;    }

Note: With the addslashes and mysql_real_escape_string methods, attackers can bypass detection by encoding, but this can be done for some simple attacks.

Prevent MySQL Injection

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