zoukankan      html  css  js  c++  java
  • PHP中magic_quotes_gpc动态关闭无效的问题

    昨天浏览线上项目,发现了一个问题:部分文本输出中的引号前多了一道反斜杠,比如:

    引号内容多了"反斜杠"

    单从页面展现的结果来看,猜测应该是PHP中的magic_quotes_gpc配置被开启了的原因。然后检查了下程序,发现在入口文件中,已经动态关闭了这个配置:

    ini_set('magic_quotes_gpc', 'Off');

    为什么没有生效呢?

    经过一番查找,同事帮忙找到了原因,原来是因为在我动态修改这个配置之前,请求已经被解析了,因此该修改并未针对当次请求生效。

    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);
    }
  • 相关阅读:
    c# 一段生成6位不重复的随机数字码存8万个
    element ui 踩坑记
    Vue node.js 踩坑记
    javascript 异步回调链式调用 promise
    css 盒模型
    vue node.js 引入 linq
    Vue VsCode 项目 launch.json 文件
    node.js 基本语法识记
    Vue 2.0 入门示例识记
    在Windows系统中建立一个隐藏的帐户(在不登录界面显示)
  • 原文地址:https://www.cnblogs.com/wicub/p/3305686.html
Copyright © 2011-2022 走看看