PHP adds a backslash (PHP removes the backslash) before quotation marks _php tips

Source: Internet
Author: User

The server space provided by the general space provider default PHP instruction MAGIC_QUOTES_GPC is on, which is open. You can then use the stripslashes () function to remove the automatically added backslash. Usage is: For example, the variable containing the string is $STR, then use the stripslashes () function to handle this string: Stripslashes ($STR), the output is the result of removing the backslash.

If the output contains a backslash, the output can be processed with the stripslashes () function, that is, $str=stripslashes ($STR), save to remove the backslash contained in the output.

But there is another problem, that is because the local PHP directive MAGIC_QUOTES_GPC is off, if this function, it will be the normal backslash also removed. This is not what we hope for.

The solution is to use the function GET_MAGIC_QUOTES_GPC () for detection, if the state is open, then remove the backslash, if the state is closed, do not remove the backslash.

The program code is as follows:

Copy Code code as follows:

$str =$_post["str"]; Read the content assignment of STR to the $STR variable
if (GET_MAGIC_QUOTES_GPC ()) {//if GET_MAGIC_QUOTES_GPC () is open
$str =stripslashes ($STR); To process a string
}

This article was amended on April 25, 2012 10:08:03 as follows:

Here are three ways to solve this problem:

1, modify the PHP configuration file php.ini

This method is only suitable for the right to manage the server, if the use of virtual space, it can only use the latter two methods.

MAGIC_QUOTES_GPC, Magic_quotes_runtime, magic_quotes_sybase are all set to off in the PHP configuration file php.ini. As shown below:

Copy Code code as follows:

MAGIC_QUOTES_GPC = Off
Magic_quotes_runtime = Off
Magic_quotes_sybase = Off

2 use of. htaccess files

This method is only supported by the server in the case of htaccess, now the server will generally support

Add the following sentence in the program directory. htaccess file:

Php_flag MAGIC_QUOTES_GPC off

3 Shielding in code

This method is the most portable, regardless of server configuration, as long as the support of PHP can be used.

Add the following code at the beginning of all PHP files

Copy Code code as follows:

if (GET_MAGIC_QUOTES_GPC ()) {
function Stripslashes_deep ($value) {
$value =is_array ($value) array_map (' Stripslashes_deep ', $value): Stripslashes ($value);
return $value;
}
$_post=array_map (' Stripslashes_deep ', $_post);
$_get=array_map (' Stripslashes_deep ', $_get);
$_cookie=array_map (' Stripslashes_deep ', $_cookie);
$_request=array_map (' Stripslashes_deep ', $_request);
}

Related Article

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.