【问题标题】:Why does $_SESSION contain the untrimmed $_POST?为什么 $_SESSION 包含未修剪的 $_POST?
【发布时间】:2014-04-23 20:27:52
【问题描述】:

为什么这不起作用?

session_start();

print("<pre>".print_r($_POST['foo'],true)."</pre>");  // 'Bob     '

array_walk_recursive($_POST, function (&$val) { $val = trim($val); });
print("<pre>".print_r($_POST['foo'],true)."</pre>");  // 'Bob'

$_SESSION['foo'] = filter_input(INPUT_POST, 'foo', FILTER_SANITIZE_STRING);
print("<pre>".print_r($_SESSION['foo'],true)."</pre>");  // 'Bob     '

【问题讨论】:

  • FILTER_SANITIZE_STRING 不修剪空白字符:Strip tags, optionally strip or encode special characters. 为什么从$_POST 切换到INPUT_POST?这种不一致是在自找麻烦。
  • 我怀疑filter_input() 的数据不是来自$_POST,而是来自$_POST 最初所在的位置。也就是说,更改$_POST 的内容不会对filter_input() 的行为产生任何影响。
  • @JohnConde 是的,但是我在通过filter_input() 之前已经修剪了 $_POST。

标签: php session post multidimensional-array trim


【解决方案1】:

来自first comment in the manual

请注意,此函数实际上并没有(或至少似乎没有)根据 $_GET 等的当前值进行过滤。相反,它似乎根据原始值进行过滤。

编辑

您可以在此处将呼叫添加到trim()

$_SESSION['foo'] = trim(filter_input(INPUT_POST, 'foo', FILTER_SANITIZE_STRING));

【讨论】:

  • 所以我不应该修剪$_POST,而是修剪$_SESSION
  • 只需将它包裹在您对filter_input() 的调用中。请参阅我的更新答案。
  • 我有很多变量。进行所有消毒后,array_walk_recursive($_SESSION, function (&amp;$val) { $val = trim($val); }); 有问题吗?
猜你喜欢
  • 2012-07-26
  • 1970-01-01
  • 2012-07-20
  • 1970-01-01
  • 1970-01-01
  • 2019-02-25
  • 2016-04-07
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多