通过预定义密码进行PHP表单验证


PHP Form Verification by Predetermined Passwords

我正在尝试创建一个简单的PHP表单,用户必须输入六个预定密码中的一个,这些密码将是TSS01、TSS02、TSS03、TSS04、TSS05和TSS06。

仅使用数字密码时,表单当前正常工作。

PHP编码如下:

<?php
if(empty($_POST['name']) || empty($_POST['code'])) {
die(print "<meta http-equiv='"refresh'" content='"0;URL=error.html'">");
}
$name=$_POST['name'];
$code=$_POST['code'];
$to='email@email.com';
$headers = 'From: '.$name."'r'n" .
'Reply-To: '.$name."'r'n" .
'X-Mailer: PHP/' . phpversion();
$subject = 'SECRET SUPPER FORM ENTRY';
$body='Entry References'."'n'n";
$body.='Name: '.$name."'n";
$body.='Code: '.$code."'n";
if( !empty( $_POST['code'] ) && $_POST['code'] == TSS01) {
$submit = mail($to, $subject, $body, $headers, "From: <$name>");
print "<meta http-equiv='"refresh'" content='"0;URL=success.html'">";
} else {
print "<meta http-equiv='"refresh'" content='"0;URL=error.html'">";
}
?>

如何通过这些密码中的任意六个来验证此表单?密码已经设置好,不能更改,必须是正确的密码。

您可以执行以下操作:

 $passwords = array('TSS01', 'TSS02', 'TSS03', 'TSS04', 'TSS05', 'TSS06');
// !empty( $_POST['code'] ) - you dont need to check again if is empty, you already do it on the firsts lines
if( in_array($_POST['code'], $passwords)) {
    // Passwords is ok
}