我创建了一个小型 PHP 脚本,它在 PHP 5.2.17 且启用了 magic_quotes_gpc
指令的服务器上运行。
我没有 php.ini 文件的写入权限,并且我想删除用户输入中的所有斜杠。
即使 magic_quotes_gpc
指令关闭(例如将脚本移动到另一台服务器时),这也应该起作用。
当用户提交数组时,它也应该递归地工作。
我更喜欢使用内置函数。
<html>
<head>
<title>HP</title>
</head>
<body>
<form method="POST" action="magic.php">
<input type="text" value="te\\"st" name="test1">
<input type="text" value="te\\"st" name="test2[tw"o]">
<input type="submit" value="submit">
</form>
<?php
echo "<pre>";
echo "magic_quotes: ".get_magic_quotes_gpc()."\n";
echo "<hr>test1";
echo "filter_input: ".filter_input(INPUT_POST, "test1")."\n";
echo "POST: ".$_POST['test1']."\n";
echo "<hr>test2 (filter)";
print_r(filter_input_array(INPUT_POST))."\n";
echo "<hr>test2 (post)";
print_r($_POST)."\n";
echo "</pre>";
?>
</body>
</html>
这在我的服务器上给出了以下结果:
magic_quotes: 1
filter_input: te\\"st
POST: te\\\\\"st
test2 (filter)Array
(
[test1] => te\\"st
[test2] => Array
(
[tw\"o] => te\\"st
)
)
test2 (post)Array
(
[test1] => te\\\\\"st
[test2] => Array
(
[tw\"o] => te\\\\\"st
)
)
看起来除了数组键之外,斜杠都被删除了。
或者斜杠从未添加过? (filter_input()
和 filter_input_array()
可能会忽略 magic_quotes_gpc
指令,因为它已被弃用;但我找不到相关引用)
删除/不设置 filter_input()
和 filter_input_array()
斜杠的行为是否在某种程度上依赖于系统参数?
我不明白警告 here .
最佳答案
我在官方文档中没有成功找到它,但 filter_input()
函数对原始数据进行操作,并且不受 magic_quotes
设置的影响。如果您需要的话,清理过滤器 FILTER_SANITIZE_MAGIC_QUOTES
会将它们放入。
这对我个人来说是一个福音,因为我正在一个启用了 magic_quotes
的旧系统中工作。通过使用 filter_input()
函数,我可以使用这些值,而无需在将它们绑定(bind)到 PDO
中之前去除斜杠。
这些文章讨论了它:
http://www.sitepoint.com/forums/showthread.php?590848-Filter_input-magic-quotes
https://weston.ruter.net/2013/10/22/revelations-about-filter_input/
http://php.net/manual/en/function.filter-input.php#99124
关于php - filter_input() 何时删除 POST 变量的斜杠?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9533122/