Home  >  Article  >  Backend Development  >  Why is magic_quotes_gpc dynamically closed invalid?

Why is magic_quotes_gpc dynamically closed invalid?

小云云
小云云Original
2018-02-24 14:41:191395browse

This article mainly shares with you the problem of invalid dynamic closing of magic_quotes_gpc in PHP, hoping to help you solve such problems.

Browsing online projects yesterday, I found a problem: there is an extra backslash before the quotation marks in some text output, for example:

There is an extra "backslash" in the quotation marks

Judging from the results displayed on the page, it is guessed that the magic_quotes_gpc configuration in PHP is turned on. Then I checked the program and found that in the entry file, this configuration has been dynamically closed:

ini_set('magic_quotes_gpc', 'Off');

Why didn't it take effect?

After some searching, my colleagues helped me find the reason. It turned out that the request had been parsed before I dynamically modified the configuration, so the modification did not take effect for the current request.

See the following webpage for details. A colleague also encountered the same problem:

https://bugs.php.net/bug.php?id=32867

magic_quotes_gpc is applied while parsing the request before your PHP script gets control so while you can change this setting in your script, it won't have any effect.

Given that there are multiple projects on the server, in order not to affect other project, we cannot directly modify the configuration of php.ini, so we used the code written by Molu vs Zhuihui to recursively process the gpc content:

if (ini_get('magic_quotes_gpc')) {
function stripslashesRecursive(array $array)
{
foreach ($array as $k => $v) {
if (is_string($v)) {
$array[$k] = stripslashes($v);
} else if (is_array($v)) {
$array[$k] = stripslashesRecursive($v);
}
}
return $array;
}
$_GET = stripslashesRecursive($_GET);
$_POST = stripslashesRecursive($_POST);
}

Related recommendations:

php get_magic_quotes_gpc( )Function usage instructions

About php automatic escaping issues, historical issues left over by magic_quotes_gpc in the configuration,

php get_magic_quotes_gpc function usage introduction

The above is the detailed content of Why is magic_quotes_gpc dynamically closed invalid?. For more information, please follow other related articles on the PHP Chinese website!

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