您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

PHP中magic_quotes_gpc动态关闭无效的问题

2025/9/10 19:35:10发布41次查看
昨天浏览线上项目,发现了一个问题:部分文本输出中的引号前多了一道反斜杠,比如:
引号内容多了\反斜杠\
单从页面展现的结果来看,猜测应该是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的配置,因此采用了陌路vs追忆编写的代码,递归处理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);}
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product