昨天瀏覽線上項目,發現了一個問題:部分文本輸出中的引號前多了一道反斜線,比如:
引號內容多了\"反斜線\"
單從頁面展現的結果來看,猜測應該是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);
}
http://www.bkjia.com/PHPjc/363784.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/363784.htmlTechArticle昨天瀏覽線上項目,發現了一個問題:部分文本輸出中的引號前多了一道反斜線,比如: 引號內容多了\反斜線\ 單從頁面展現的結果來看,...