Home > Backend Development > PHP Tutorial > php addslashes and mysql_real_escape_string_PHP tutorial

php addslashes and mysql_real_escape_string_PHP tutorial

WBOY
Release: 2016-07-21 15:41:34
Original
915 people have browsed it

It is a good explanation of the difference between addslashes and mysql_real_escape_string. Although many domestic PHP coders still rely on addslashes to prevent SQL injection (including me), I still recommend that everyone strengthen checks to prevent SQL injection in Chinese. The problem with addslashes is that hackers can use 0xbf27 to replace 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:

Copy the code The code is as follows:

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:
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.

To summarize:

addslashes() is a forced addition;
mysql_real_escape_string() will determine the character set, but there are requirements for the PHP version;
mysql_escape_string does not consider the current characters of the connection set.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/321188.htmlTechArticle explains the difference between addslashes and mysql_real_escape_string very well, although many domestic PHP coders still rely on addslashes to prevent SQL injection ( Including me), I still recommend that everyone strengthen their...
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