For a long time, the security of web has great controversy and challenge. Among them, SQL injection is a common attack method, the common practice of developers is to constantly filter, escape parameters, but our PHP Dafa inherently weak type of mechanism, always let hackers have the advantage, bypassing defense and defense is always in the infighting.
Brother Lian (www.itxdl.cn) PHP Daniel said a word, in a program, 60% of the code should be in a variety of defense.
In fact, now, the defense of SQL injection does not need to do a variety of parameter filtering, the following will open dry mode!
Php5.x began to introduce a new mode of operation of MySQL-----mysqli, in PHP also has a corresponding mode of operation called PHP preprocessing. The use of object-oriented approach to parameterized binding operations, because of the database operation of the mode-driven different, so it can be very effective defense of SQL injection.
First, let's take a look at some code examples
PHP Code:
<!--? php
$root = "root";
$pwd = "root";
$host = "localhost";
$database = "Database";
$conn = new Mysqli ($host, $root, $pwd, $database);//Instantiate an object in an object-oriented way
$keywords = $_get[' keywords ');
$search _sql = "Select content from MyKey where title =?"; /One of the? is a placeholder
$search _action = $conn--->prepare ($search _sql);//pre-processing operation
$search _action->bind_param ("s", $keywords);//binding parameters, the first parameter is represented as the number of pre-preprocessed placeholders and the data type of each parameter, S is a string, I is shaping, D is a double-precision decimal, there are several parameters, Just write a few s or D or I, such as Iiii,ssss,sidi. Then there are a few parameters to write several variables to bind, such as Bind_param (' SSS ', $username, $password, $code);
$search _action->bind_result ($content);//Bind the result to a relative variable, such as if you select Username,password, you can write Bind_result ($ Usernmae, $password);
$search _action->execute ();//Execute SQL operation
while ($search _action->fetch ()) {
echo $content. ' <br> ';
}
$search _action->free_result ();//Release memory
$search _action->close ();//end of this instantiation
?>
The above is a very simple example of PHP preprocessing, its built-in other functions can be very convenient for our development speed, then see here, many people may still do not understand, someone may want to ask, you this binding parameter is still in the patchwork SQL statement? If it's a patchwork of statements, wouldn't that have been injected?
This will be from his operating principle to explain, in fact, it in the prepare operation, it has been in the database, executed the statement, after the binding parameters and execution, but only to pass the data into it, so there is no connection with the SQL statement, it will naturally not be dangerous code execution. Therefore, SQL injection can be effectively defended in this mode.
In the PHP pre-processing class has a lot of good operation, the specific brothers will be in later articles for you to summarize some common PHP preprocessing development statements.
PHP's defense of SQL injection attack methods