Home > PHP Framework > ThinkPHP > body text

How thinkphp prevents sql injection

藏色散人
Release: 2019-08-05 13:30:55
Original
10370 people have browsed it

How thinkphp prevents sql injection

How does thinkphp prevent sql injection?

For WEB applications, SQL injection attacks are undoubtedly the primary security issue to prevent. The bottom layer of the system has done a lot of processing and corresponding prevention mechanisms for data security, such as:

$User = M("User"); // 实例化User对象
$User->find($_GET["id"]);
Copy after login

Even if the user enters some malicious id parameters, the system will force conversion to an integer to avoid malicious injection. This is because the system will perform mandatory data type detection on the data and perform data format conversion on the data source. Moreover, for string type data, ThinkPHP will perform escape_string processing (real_escape_string, mysql_escape_string), and also supports parameter binding.

The usual security risk is that your query conditions use string parameters, and then some of the variables rely on user input from the client.

To effectively prevent SQL injection problems, we recommend:

● Try to use arrays for query conditions, which is a safer way;

● You must use it if you have to For string query conditions, use the preprocessing mechanism;

● Use automatic verification and automatic completion mechanisms for customized filtering for applications;

● If the environment permits, try to use PDO and use Parameter binding.

Query condition preprocessing

When the where method uses string conditions, it supports preprocessing (security filtering) and supports two methods of passing in preprocessing parameters. For example:

$Model->where("id=%d and username='%s' and xx='%f'",array($id,$username,$xx))->select();
// 或者
$Model->where("id=%d and username='%s' and xx='%f'",$id,$username,$xx)->select();
Copy after login

The query and execute methods of the model also support the preprocessing mechanism. For example:

$model->query('select * from user where id=%d and status=%d',$id,$status);
//或者
$model->query('select * from user where id=%d and status=%d',array($id,$status));
Copy after login

The execution method is used the same as the query method.

This article comes from the ThinkPHP framework technical article column: http://www.php.cn/phpkj/thinkphp/

The above is the detailed content of How thinkphp prevents sql injection. 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