for a long time, the security ofWeb 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.lampbrother.net
PHP Daniel said a word, in a program,
40g
The code should be in a variety of defenses.
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 started to introduce a new mode of operation of MySQL -----mysqli, in PHP also has a corresponding operation is 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);// object-oriented way to instantiate an object
$keywords = $_get[' keywords ');
$search _sql = "Select content from MyKey wheretitle =?"; / One of them ? is a placeholder
$search _action = $conn--->prepare ($search _sql);// Perform preprocessing operations
$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 a few s or d or I, for example 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 ();// Freeing 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, is already in the database, executed the statement, after the binding parameters and execution, but is to pass the data in only, so there is no connection with the SQL statement, It is also natural that dangerous code will not be executed. 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