You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PHP表单多值密码验证异常:输入数组有效值却验证失败如何解决?

解决表单密码数组匹配问题

首先,咱们先揪出问题的根源:你现在用$enter === $pass来做判断,这是把用户输入的字符串和整个数组做严格相等比较——一个是字符串类型,一个是数组类型,它们永远不可能相等,所以不管你输入"Batanes"还是"batanes",都会验证失败。

要实现「输入值匹配数组中任意一项就执行成功逻辑」,可以用这几种方法:

方法1:直接使用in_array()函数

PHP的in_array()函数专门用来检查某个值是否存在于数组中,刚好符合你的需求。把原来的判断条件替换成:

if (in_array($enter, $pass)) {
    // 成功逻辑
}

因为你的$pass数组里已经包含了"Batanes"和"batanes"两种大小写形式,所以不管用户输入哪种,in_array()都会返回true,触发成功逻辑。

方法2:统一大小写(更优雅的写法)

如果不想在数组里存重复的大小写值,可以统一把用户输入和数组元素转成小写(或大写)再比较,这样数组只需要存一个值,代码更简洁:

// 数组只存一种形式
$pass = array("batanes");
$enter = strtolower($_POST['Pass']); // 将用户输入转成小写

if (in_array($enter, $pass)) {
    // 成功逻辑
}

这种方法还能避免因为数组漏写某个大小写形式导致的验证失败,更健壮。

修改后的完整代码示例

这里用方法1给你修改好完整代码:

<form id="pass" method="POST">
    <b><label for="Pass"> Where is our Ultimate Travel Destination Wish List in the Philippines? </label></b>
    <input id="Pass" name="Pass" type="text" placeholder="Enter Password Here..." required>
    <input type=submit>
</form>
<?php 
error_reporting(0); 
$pass = array("Batanes", "batanes"); 
$enter = $_POST['Pass'] ?? ''; // 用??避免未提交时的Undefined index警告

if (in_array($enter, $pass)) { 
    echo <<<EOL
    <script>
        $('<div class=overlay></div>').bind('mouseover',function(){ });
        $('<div class="popup"> Good Job Man! <br> Now to the next! </div>').bind('mouseout', function(){ });
        setTimeout(function(){ window.location.href = 'mommysthirdimahe.html'; }, 2000);
    </script>
EOL; 
    exit; 
} 
?>

另外还加了$_POST['Pass'] ?? ''来避免用户还没提交表单时出现Undefined index: Pass的警告,让代码更严谨。

内容的提问来源于stack exchange,提问作者EValo

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.07 15:03:10