Sharing a simple and efficient way to defend against SQL injection in PHP

*文
Release: 2023-03-18 09:36:01
Original
2506 people have browsed it

WEB security has always been a very serious topic. SQL injection is a common attack method. Many times, our code is designed to process non-compliant data and prevent injection. But as a weakly typed language, PHP always has risks that we fail to consider. This article shares a simple but effective method to prevent SQL injection!

I remember something I saw a great expert said a long time ago. In a program, 60% of the code should be for various defenses.

In fact, looking at it now, preventing sql injection does not actually require various parameter filtering. The dry information mode will be turned on below!

PHP5.x began to introduce a new mysql operation method-----mysqli. There is also a corresponding operation method in PHP called PHP preprocessing. The object-oriented approach is used to perform parameterized binding operations. Due to the different mode drivers for database operations, it can be very effective in preventing SQL injection.

First, let’s take a look at a code example

<!--?php 
$root = "root";
$pwd = "root";
$host = "localhost";
$database =  "database";
$conn = new mysqli($host,$root,$pwd,$database);//面向对象的方式实例化一个对象
$keywords = $_GET[&#39;keywords&#39;];
$search_sql = "select content from mykey where title = ? ";//其中的?是一个占位符
$search_action = $conn --->prepare($search_sql);//进行预处理操作
$search_action ->bind_param("s",$keywords);//绑定参数,第一个参数表示为上面预处理的的占位符的数量和每一个参数的数据类型,s为字符串,i为整形,d为双精度小数,有几个参数,就写几个s或d或i,比如说iiii,ssss,sidi这样的。然后后面就是有几个参数就写几个要绑定的变量,比如bind_param(&#39;sss&#39;,$username,$password,$code);
$search_action ->bind_result($content);//将结果绑定在相对应的变量上,比如你select了username,password,你就可以写bind_result($usernmae,$password);
$search_action ->execute();//执行sql操作
while($search_action ->fetch()){
echo $content.&#39;<br>&#39;;
}
$search_action ->free_result();//释放内存
$search_action ->close();//结束这个实例化
?>
Copy after login

The above is a very simple example in PHP preprocessing. Its built-in other functions can very convenient for our development speed, so see Many people may still not understand here. Some may want to ask, are you still piecing together SQL statements with this binding parameter? If it is a pieced together statement, wouldn't it cause injection?

This needs to be explained from its operating principle. In fact, during the prepare operation, the statement has already been executed in the database, and subsequent binding Setting parameters and executing is just a matter of passing data in, so it will not be spliced ​​with SQL statements at all, so dangerous code will naturally not be executed. Therefore, SQL injection can be effectively defended in this mode.

There are many useful operations in the PHP preprocessing class. I will summarize some commonly used PHP preprocessing development statements for you in future articles.

Related recommendations:

php sql injection and anti-injection classic case analysis_PHP tutorial

Discuz7.2 version of faq.php SQL injection vulnerability analysis, discuz7.2faq.php_PHP tutorial

php sql injection attack and prevention Precautions

The above is the detailed content of Sharing a simple and efficient way to defend against SQL injection in PHP. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!