为什么strpos+strlen在PHP中不安全(例如:精确匹配)


Why strpos + strlen is not secure in PHP (ex: exact matching)

我和老师讨论了mb_函数。不管怎样,一件事导致另一件事,我们改变了话题,他给了我一个strposstrlen可能有问题的例子,据他说:

$input  = "something"; # input given by the user
$string = "hello";     # string to match
if ( strpos($input, $string) !== false && strlen($input) < strlen($string) ) {
    echo "Correct input";
} else {
    echo "Incorrect input";
}

(问题不在于如何匹配2个字符串)

根据我的老师的说法,可能有一种方法可以验证该语句并执行代码echo "Correct input";

然而,我看不出这里面有什么瑕疵。也许编码有问题?你知道吗?

好的,我认为缺陷将是if语句逻辑

如何使用strpos函数来检查字符串中第一个子字符串的位置,同时检查输入是否大于主题?

从逻辑上讲,是不可能的