Very good description of the difference between addslashes and mysql_real_escape_string, although many of the domestic PHP coder still rely on addslashes to prevent SQL injection (including me), I suggest that we strengthen the Chinese to prevent SQL injection check. The problem with addslashes is that hackers can use 0xbf27 instead of single quotes, and addslashes only modifies 0xbf27 to 0xbf5c27 as a valid multi-byte character, where the 0xbf5c is still considered single quotes, So addslashes cannot successfully intercept.
Of course, addslashes is also not useless, it is used for single-byte string processing, multibyte characters or use mysql_real_escape_string bar.
Also for examples of GET_MAGIC_QUOTES_GPC in the PHP manual:
Copy Code code as follows:
if (!GET_MAGIC_QUOTES_GPC ()) {
$lastname = addslashes ($_post[' LastName '));
} else {
$lastname = $_post[' LastName '];
}
It is better to check the $_post[' LastName ' If the MAGIC_QUOTES_GPC is already open.
Again, the difference between the 2 functions of mysql_real_escape_string and mysql_escape_string:
Mysql_real_escape_string must be available in the case of (PHP 4 >= 4.3.0, PHP 5). Otherwise you can only use mysql_escape_string, the difference is:
Mysql_real_escape_string takes into account the current character set of the connection, and Mysql_escape_string does not consider it.
To sum up:
Addslashes () is forcibly added;
Mysql_real_escape_string () will judge the character set, but the PHP version is required;
Mysql_escape_string does not consider the current character set of the connection.