PHP in front of quotes to add the reason for the backslash and PHP to remove the backslash, three ways to turn off the PHP magic quotes

Source: Internet
Author: User
Tags ini variable

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:

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

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

Here are three ways to solve this problem:

1 Modify 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:

1 MAGIC_QUOTES_GPC = Off
2
3 Magic_quotes_runtime = Off
4
5 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:

1 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

01 if (GET_MAGIC_QUOTES_GPC ()) {
02 function Stripslashes_deep ($value) {
03 $value =is_array ($value) array_map (' Stripslashes_deep ', $value): Stripslashes ($value);
04 return $value;
05 }
06 $_post=array_map (' Stripslashes_deep ', $_post);
07 $_get=array_map (' Stripslashes_deep ', $_get);
08 $_cookie=array_map (' Stripslashes_deep ', $_cookie);
09 $_request=array_map (' Stripslashes_deep ', $_request);
10 }


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.