Addslashes, mysql_real_escape_string and mysql_escape_string

Source: Internet
Author: User

This article briefly introduces some usage and differences between addslashes (), mysql_real_escape_string (), and mysql_escape_string () in php. For more information, see.

I have never paid attention to this before. When writing, you use a very simple function, addslashes (), to add a backslash before a specified predefined character.

The predefined characters are:

• Single quotes (')
• Double quotation marks (")
• Backslash ()
• NULL

The Code is as follows: Copy code

<? Php
Function as_array (& $ arr_r)
{
Foreach ($ arr_r as & $ val) is_array ($ val )? As_array ($ val): $ val = addslashes ($ val );
Unset ($ val );
}

As_array ($ _ POST );
?>


Although many PHP programmers in China are still relying on addslashes to prevent SQL injection, we recommend that you enhance Chinese characters to prevent SQL Injection checks. The problem with addslashes is that hackers can use 0xbf27 to replace single quotes, while addslashes only changes 0xbf27 to 0xbf5c27 to a valid multi-byte character, where 0xbf5c is still considered as single quotes, therefore, addslashes cannot be intercepted.

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

In addition, the example of get_magic_quotes_gpc in the php manual is as follows:

The Code is as follows: Copy code

If (! Get_magic_quotes_gpc ()){
$ Lastname = addslashes ($ _ POST ['lastname']);
} Else {
$ Lastname = $ _ POST ['lastname'];
}

If magic_quotes_gpc is enabled, check $ _ POST ['lastname.

Let's talk about the differences between the two functions mysql_real_escape_string and mysql_escape_string:
Mysql_real_escape_string can be used only when (PHP 4> = 4.3.0, PHP 5. Mysql_escape_string (PHP 4> = 4.0.3, PHP 5, note: This method has been deprecated in PHP5.3 and is not recommended ),

<? Php
// Description: Use array_map () to call mysql_real_escape_string to clear the array.
// Finishing: http://www.bKjia. c0m
Function mysqlClean ($ data)
{
Return (is_array ($ data ))? Array_map ('mysqlclean', $ data): mysql_real_escape_string ($ data );
}
?>

The difference between the two is: mysql_real_escape_string considers the current character set to be connected, while mysql_escape_string does not.

Summary:

PHP code

The Code is as follows: Copy code

<? Php
Function escape ($ str ){
If (function_exists ('mysql _ escape_string ')){
Return mysql_escape_string ($ str );
} Elseif (function_exists (... real_escape ...)){
// Real_escape
} Else {
If (MAGIC_QUOTER... judgment ){
Return $ str
} Else {
Return addslashes ($ str );
}
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.