Force users to type specific word in php form

I have this nicely working form code for my website, but want to add my own simple captcha.

I want to add another field (which I can do) that forces users to type out what they see in an image I will insert - so there is only one right answer.

If, for example, telephone number required the numbers ‘1234’ to be added, how do I force users to do this?

Many thanks in advance!

[php]<?php
if(isset($_POST[‘submit’])) {

$name = $_POST['name']; // required
$telephone_number = $_POST['telephonenumber']; // required
$email_address = $_POST['emailaddress']; // required										
$email_address_confirm = $_POST['emailaddressconfirm']; // required
$comments = $_POST['comments']; // required
 
if($name=='' || 

$telephone_number==’’ ||
$email_address==’’ ||
$email_address_confirm==’’ )

{  
    die('You have not filled in all of the required fields - please hit your back button and try again!');

}

else{
	$email_to = "[email protected]";
	$email_subject = "Contact from My Company";
	$email_message = "Form details below.\n\n";
 
function clean_string($string) {
  $bad = array("content-type","bcc:","to:","cc:","href");
  return str_replace($bad,"",$string);
}

$email_message .= "Name or Company Name: ".clean_string($name)."\n";
$email_message .= "Telephone Number: ".clean_string($telephone_number)."\n";				
$email_message .= "Email Address: ".clean_string($email_address)."\n";
$email_message .= "Confirm Email Address: ".clean_string($email_address_confirm)."\n";
$email_message .= "Comments: ".clean_string($comments)."\n";

// create email headers
$headers = ‘From: My Company’.
//'Reply-To: '.$email_from."\r\n" .
‘X-Mailer: PHP/’ . phpversion();
@mail($email_to, $email_subject, $email_message, $headers);
}
}
?>
[/php]

It doesn’t sound like you’re going to be using a random or a set of images.

If this is true you would simply need to create the variables for what the user types in and one for the characters that are on the image. Then check to make sure they are both the same.

So if the image says FOO and the text area name is captcha…

[php]
$var1 = $_POST[‘captcha’];
$var2 = ‘F00’;

if($var1 !== $var2){
die(‘Catpcah Check Failed You Dirty Bot’);
};
[/php]

Thanks for that, but I don’t know how to insert into my code without breaking it!

Sorry about that.

I would use an elseif statement after you check for empty strings.

[php]

<?php if(isset($_POST['submit'])) { $name = $_POST['name']; // required $telephone_number = $_POST['telephonenumber']; // required $email_address = $_POST['emailaddress']; // required $email_address_confirm = $_POST['emailaddressconfirm']; // required $comments = $_POST['comments']; // required if($name=='' || $telephone_number=='' || $email_address=='' || $email_address_confirm=='' ) { die('You have not filled in all of the required fields - please hit your back button and try again!'); } elseif('$var1 !== $var2') //Repalce with Captcha Variables. { die('Catpcah Check Failed You Dirty Bot'); } else{ $email_to = "[email protected]"; $email_subject = "Contact from My Company"; $email_message = "Form details below.\n\n"; function clean_string($string) { $bad = array("content-type","bcc:","to:","cc:","href"); return str_replace($bad,"",$string); } $email_message .= "Name or Company Name: ".clean_string($name)."\n"; $email_message .= "Telephone Number: ".clean_string($telephone_number)."\n"; $email_message .= "Email Address: ".clean_string($email_address)."\n"; $email_message .= "Confirm Email Address: ".clean_string($email_address_confirm)."\n"; $email_message .= "Comments: ".clean_string($comments)."\n"; // create email headers $headers = 'From: My Company'. //'Reply-To: '.$email_from."\r\n" . 'X-Mailer: PHP/' . phpversion(); @mail($email_to, $email_subject, $email_message, $headers); } } ?>

[/php]

Let me know if that works or not.

Nope, sorry I don’t understand - what does “Replace with Captcha Variables” mean?

As I said, I don’t understand php enough to add anything.

Anyone else available with any ideas? I think it is a matter of adjusting this area if I want to create a mandatory static word - possibly with (it’s been suggested) “== is comparison” ?

[php] if($name==’’ ||
$telephone_number==’’ ||
$email_address==’’ ||
$email_address_confirm==’’ )

{  
    die('You have not filled in all of the required fields - please hit your back button and try again!');

}[/php]

You have already been given the correct answer. Can you post your current code including the form HTML?

Php code already posted, here’s the html form. I don’t know how else to best explain it, I don’t know php enough to add things in, only to adjust it once it’s there.

[code]

Name or Company Name*
Email Address*
Confirm Email Address*
Telephone Number*
Please provide us with some further information:

Which of the following is of interest?*

Web Design Logo Design Copywriting

Other (please add comments)

Please add any further comments / information below:


[/code]

Where is your form is your required field?

Don’t understand the question. PHP form code is posted in original post, html code is posted just above.

Sponsor our Newsletter | Privacy Policy | Terms of Service