PHP 验证码验证:四个输入框校验数字并跳转
以下是一个简单的 PHP 代码示例,用于验证四个输入框中的数字是否等于 '1111',如果不等于则提示错误并清空输入框,如果等于则跳转到指定页面。
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$input1 = test_input($_POST["input1"]);
$input2 = test_input($_POST["input2"]);
$input3 = test_input($_POST["input3"]);
$input4 = test_input($_POST["input4"]);
if ($input1 == 1 && $input2 == 1 && $input3 == 1 && $input4 == 1) {
header("Location: http://www.example.com"); //跳转到指定页面
exit();
} else {
echo '验证码错误,请重新输入。';
$input1 = "";
$input2 = "";
$input3 = "";
$input4 = "";
}
}
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
$data = is_numeric($data) ? $data : ""; //检查输入是否为数字
return $data;
}
?>
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
<input type="text" name="input1" maxlength="1" size="1">
<input type="text" name="input2" maxlength="1" size="1">
<input type="text" name="input3" maxlength="1" size="1">
<input type="text" name="input4" maxlength="1" size="1">
<input type="submit" name="submit" value="提交">
</form>
该代码使用 POST 方法提交表单,获取四个输入框的值,并进行验证。如果验证通过,则跳转到指定页面;否则提示错误并清空输入框。
test_input() 函数用于对输入进行过滤,确保输入为数字。该代码仅供参考,可以根据实际需求进行修改。
原文地址: https://www.cveoy.top/t/topic/nOug 著作权归作者所有。请勿转载和采集!