0

I am trying to create a simple PHP form where the user must enter one of six predetermined passwords, these will be TSS01, TSS02, TSS03, TSS04, TSS05 and TSS06.

The form currently functions properly when using only numeric passwords.

PHP coding follows:

<?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 protected]';

$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\">";

}
?>

How can this form be verified by any six of these passwords? The passwords are already set and cannot be changed, they must be those exactly.

2
  • You forgot the quotation marks around TSS01. Try $_POST['code'] == 'TSS01' Feb 18, 2015 at 16:53
  • When checking $_POST['code'] check for all of the passwords, not just TSS01.
    – Celeo
    Feb 18, 2015 at 16:53

1 Answer 1

0

You can do something like:

 $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
}
1
  • Thank you for your response - this works perfectly, I only had to add a string of the same passwords with lowercase text. Thank you!
    – Stanley
    Feb 18, 2015 at 17:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.