首页 > 解决方案 > How to preset $_POST for filter_input()?

问题描述

Assuming this script:

<?php $_POST['user'] = 'test'; ?>
<input type="text" name="user" value="<?php echo $_POST['user']; ?>">
<input type="text" name="user" value="<?php echo filter_input(INPUT_POST, 'user', FILTER_SANITIZE_SPECIAL_CHARS) ?>">

Why doesn't the second input see the $_POST value set on the first line and how can we make it see it?

标签: phppost

解决方案


I'm looking for a reference as I have seen it in the past, but as you show, INPUT_POST does not use the $_POST superglobal array. At least not the at the state that you have access to. Since you are adding to $_POST this won't work, you would need to use:

<?php echo filter_var($_POST['user'], FILTER_SANITIZE_SPECIAL_CHARS); ?>

推荐阅读