實現代碼:
復制代碼 代碼如下:
<?php
function addslashes_deep($value)
{
//史上最經典的遞歸,一行搞定
return is_array($value) ? array_map('addslashes_deep', $value) : addslashes($value);
}
//測試數據
$_POST['STR'] = "'fanglor ' is \ a boy >'";
$_GET['STR1'] = 'fanglor " is \ a boy >';
echo '當前get_magic_quotes_gpc為 '.get_magic_quotes_gpc();
echo "<br/>";
//判斷當前是否開啟get_magic_quotes_gpc
if (!get_magic_quotes_gpc()){
$_POST = addslashes_deep($_POST);
$_GET = addslashes_deep($_GET);
$_COOKIE = addslashes_deep($_COOKIE);
}
//打印結果
var_dump ($_POST);
echo "<br/>";
var_dump ($_GET);
?>
打印結果:
當前get_magic_quotes_gpc為 0
array(1) { ["STR"]=> string(30) "\'fanglor \' is \\ a boy >\'" }
array(1) { ["STR1"]=> string(26) "fanglor \" is \\ a boy >" }