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