PHP comes with function filtering sql injection code analysis_PHP tutorial

WBOY
Release: 2016-07-13 17:10:13
Original
921 people have browsed it

SQL injection attack is the most common method used by hackers to attack websites. If your site does not use strict user input validation, it is often vulnerable to SQL injection attacks. SQL injection attacks are usually implemented by submitting bad data or query statements to the site database, which may cause records in the database to be exposed, changed or deleted.

In order to prevent SQL injection attacks, PHP comes with a function that can process the input string and perform preliminary security processing on the input at the lower level, that is, Magic Quotes. (php.ini magic_quotes_gpc). If the magic_quotes_gpc option is enabled, single quotes, double quotes, and other characters in the input string will be automatically preceded by backslashes.

But Magic Quotes is not a very universal solution, it does not block all potentially dangerous characters, and Magic Quotes is not enabled on many servers. Therefore, we also need to use various other methods to prevent SQL injection.

Many databases themselves provide this input data processing function. For example, PHP's MySQL operation functions include addslashes(), mysql_real_escape_string(), mysql_escape_string() and other functions, which can escape special characters and characters that may cause database operation errors. So what are the differences between these three functional functions? Let’s talk about it in detail below.

Although many domestic PHP programmers still rely on addslashes to prevent SQL injection, it is recommended that everyone strengthen checks to prevent SQL injection in Chinese. The problem with addslashes is that hackers can use 0xbf27 instead of single quotes, while addslashes only changes 0xbf27 to 0xbf5c27, which becomes a valid multi-byte character. 0xbf5c is still regarded as a single quote, so addslashes cannot successfully intercept.

Of course, addslashes is not useless. It is used for processing single-byte strings. For multi-byte characters, use mysql_real_escape_string.

In addition, for the example of get_magic_quotes_gpc in the php manual:
 

Mysql_real_escape_string can only be used under (PHP 4 >= 4.3.0, PHP 5). Otherwise, you can only use mysql_escape_string. The difference between the two is: mysql_real_escape_string takes into account the current character set of the connection, while mysql_escape_string does not.
The code is as follows
 代码如下 复制代码
if (!get_magic_quotes_gpc()) {
    $lastname = addslashes($_POST[‘lastname’]);
    } else {
    $lastname = $_POST[‘lastname’];
    }
Copy code

if (!get_magic_quotes_gpc()) {

$lastname = addslashes($_POST[‘lastname’]);
} else {

$lastname = $_POST[‘lastname’];

}



It is best to check $_POST[’lastname’] when magic_quotes_gpc is already open.

Let’s talk about the difference between the two functions mysql_real_escape_string and mysql_escape_string:
To summarize:

* addslashes() is a forced addition;

* mysql_real_escape_string() will determine the character set, but there are requirements for the PHP version; The way to prevent sql injection in dz is to use the addslashes function. At the same time, some replacements are made in the dthmlspecialchars function $string = preg_replace(/&((#(d{3,5}|x[a-fA-F0 -9]{4}));)/, &1, This replacement solves the injection problem and also solves some problems with Chinese garbled characters http://www.bkjia.com/PHPjc/629675.html
www.bkjia.comtrue
http: //www.bkjia.com/PHPjc/629675.htmlTechArticleSQL injection attack is the most common method used by hackers to attack websites. If your site does not use strict user input validation, it is often vulnerable to SQL injection attacks. SQL injection attacks usually pass...
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