magic_quotes_gpc php,php的magic_quotes_gpc动态关闭无效的解决方法

线上的一个项目中,部分文本输出中的引号前多了一道反斜杠,比如:引号内容多了\"反斜杠\"。

单从页面展现的结果来看,猜测应该是PHP中的magic_quotes_gpc配置被开启了的原因。

然后检查了下程序,发现在入口文件中,已经动态关闭了这个配置:

ini_set('magic_quotes_gpc', 'Off');

为何没有生效呢?

经过一番查找,找到的原因是:动态修改此配置之前,请求已经被解析了,因此该修改并未针对当次请求生效。

这里有一个英文的参考地址,大家不妨看看:

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.

鉴于服务器上存在多个项目,为了不影响其他项目,我们也不能直接修改php.ini的配置,因此采用了陌路与追忆编写的代码,递归处理gpc内容:

复制代码 代码如下:

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);

}

?>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值