通过预先设定的密码进行PHP表单验证
我试图创建一个简单的PHP表单,其中用户必须输入六个预定密码中的一个,它们将是TSS01、TSS02、TSS03、TSS04、TSS05和TSS06 仅使用数字密码时,表单当前运行正常 PHP编码如下:通过预先设定的密码进行PHP表单验证,php,forms,passwords,verification,Php,Forms,Passwords,Verification,我试图创建一个简单的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\">"); } $na
<?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
}
您忘记了TSS01周围的引号。检查
$\u POST['code']=='TSS01'
时,请尝试检查所有密码,而不仅仅是TSS01。感谢您的回复-这非常有效,我只需添加一个带有小写文本的相同密码字符串。非常感谢。我很高兴这对你有帮助:)