filter_input 始终返回 null

问题描述 投票:0回答:1

我一定在这里做了一些根本性错误的事情,但我看不出它是什么。在发现 filter_input 到底做什么的过程中,我编写了这个测试来显示 gozintas 和 gozoutas。

for($i = 0; $i <= 255; ++$i) {
    if ($i == 0) {
        $c1 = "What@The@Heck";
    } else {
        $c1 = chr($i);
    }
    
    $_GET['c1'] = $c1;
    print "GET="; var_dump($_GET); print "<br>";
    $c2 = filter_input(INPUT_GET, 'c1');
    $c3 = filter_input(INPUT_GET, 'c1', FILTER_SANITIZE_SPECIAL_CHARS);
    $c4 = filter_input(INPUT_GET, 'c1', FILTER_SANITIZE_ENCODED);
    $c5 = filter_input(INPUT_GET, 'c1', FILTER_SANITIZE_STRING, FILTER_FORCE_ARRAY);
    
    print "i=$i c1=$c1 c2=$c2 c3=$c3 c4=$c4 c5=$c5<br>";
    print "c5=";var_dump($c5); print "<br>";
}

所有输出如下所示:

...
GET=array(1) { ["c1"]=> string(1) ">" }
i=62 c1=> c2= c3= c4= c5=
c5=NULL
GET=array(1) { ["c1"]=> string(1) "?" }
i=63 c1=? c2= c3= c4= c5=
c5=NULL
GET=array(1) { ["c1"]=> string(1) "@" }
i=64 c1=@ c2= c3= c4= c5=
c5=NULL
GET=array(1) { ["c1"]=> string(1) "A" }
i=65 c1=A c2= c3= c4= c5=
c5=NULL
GET=array(1) { ["c1"]=> string(1) "B" }
i=66 c1=B c2= c3= c4= c5=
c5=NULL
GET=array(1) { ["c1"]=> string(1) "C" }
i=67 c1=C c2= c3= c4= c5=
c5=NULL
...

这真的很令人困惑,因为我知道 filter_input 在我们代码的其他地方以相同的方式使用,没有任何问题。运行 php 8.3.11。怜悯一个精疲力竭的程序员吧。

php filter-input
1个回答
0
投票

我偶然发现了答案,这真的很令人惊讶。这是来自 php.net filter_has_var 用户在 melp dot nl 的 drm 贡献的注释。

// Please note that the function does not check the live array, it actually checks the content received by php:

$_GET['test'] = 'ABC';

echo filter_has_var(INPUT_GET,'test') ? 'Yes' : 'No';

// would say "No", unless the parameter was actually in the query string.
// Also, if the input var is empty, it will say Yes.

因此,filter_input 查看 $_GET 数组数据的某些内部版本,而不是实际的数组本身。为什么? 这可能是什么原因呢? 为什么没有记录下来? 我很快得出结论,filter_input 等尚未准备好迎接黄金时段。如果我根本不使用它并坚持使用我自己的自定义过滤器功能可能会更好。

© www.soinside.com 2019 - 2024. All rights reserved.