Home  >  Article  >  Backend Development  >  PHP and SQL injection attacks [2]_PHP tutorial

PHP and SQL injection attacks [2]_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 15:55:01685browse

PHP and SQL injection attacks [2]

Magic Quotes

As mentioned above, SQL injection mainly submits unsafe data to the database to achieve the purpose of attack. In order to prevent SQL injection attacks, PHP comes with a function that can process the input string, and can perform preliminary processing on the input security
at a lower level, that is, Magic Quotes. (php.ini magic_quotes_gpc). If the magic_quotes_gpc
option is enabled, then 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 provide this input data processing functionality natively. For example, PHP's MySQL operation function has a function called mysql_real_escape_string(), which can escape special characters and characters that may cause database operation errors.

Reference:
http://cn2.php.net/mysql_real_escape_string
If you are interested, you can read the comments below:)

Look at this code:

//If Magic Quotes function is enabled
if (get_magic_quotes_gpc()) {
$name = stripslashes($name);
}else{
$name = mysql_real_escape_string($name);
}

mysql_query(“SELECT * FROM users WHERE name=’{$name}’”);

Note that we need to judge the Magic Quotes before using the functions provided by the database. Whether to open it, just like in the above example
, otherwise an error will occur if it is processed twice. If MQ is enabled, we need to remove the added ones to get the real
data.

In addition to preprocessing the above string-form data, when storing Binary data into the database, you should also
pay attention to preprocessing. Otherwise, the data may conflict with the storage format of the database itself, causing the database to crash, data
records to be lost, or even the entire database to be lost. Some databases, such as PostgreSQL, provide a function pg_escape_bytea() specially used to encode
binary data, which can encode the data similar to Base64.

For example:
// for plain-text data use:
pg_escape_string($regular_strings);

// for binary data use:
pg_escape_bytea($binary_data) ;

In another case, we should also adopt such a mechanism. That is multi-byte languages ​​
that the database system itself does not support, such as Chinese, Japanese, etc. Some of them have ASCII ranges that overlap with binary data ranges.
However, encoding the data may cause query statements like LIKE abc% to fail.

http://www.bkjia.com/PHPjc/318373.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/318373.htmlTechArticlePHP and SQL injection attacks [2] MagicQuotes As mentioned above, SQL injection mainly involves submitting unsafe data to database to achieve the purpose of attack. In order to prevent SQL injection attacks, PHP comes with a function...
Statement:
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