Hi,
I’ve got a working php form which I want to add a captcha too - I have downloaded a nice looking simple one but cannot work out how to include it into the existing php code?
Here is the php for my form:
[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=='' || // correspond to html names or maybe 1st field in A - these are the required fields
$email_address=='' ||
$email_address_confirm=='' ||
$comments==' ')
{
die('You have not filled in all of the required fields - please hit your back button and try again!');
}
else{
$email_to = "info@email";
$email_subject = "Contact from 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 = "MIME-Version: 1.0" . "\r\n";
$headers .= "From: fizzwebdesign@email" . "\r\n";
$headers .= "Reply-To: info@email" . "\r\n";
$headers .= "Content-type:text;charset=iso-8859-1" . "\r\n";
mail($email_to, $email_subject, $email_message, $headers);
}
}
?>
[/php]
& here’s the captcha code:
[php]
<?php
/** Validate captcha */
if (!empty($_REQUEST['captcha'])) {
if (empty($_SESSION['captcha']) || trim(strtolower($_REQUEST['captcha'])) != $_SESSION['captcha']) {
$captcha_message = "Invalid captcha";
$style = "background-color: #FF606C";
} else {
$captcha_message = "Valid captcha";
$style = "background-color: #CCFF99";
}
$request_captcha = htmlspecialchars($_REQUEST['captcha']);
echo <<<HTML
$captcha_message
Session CAPTCHA: |
{$_SESSION['captcha']} |
Form CAPTCHA: |
$request_captcha |
HTML;
unset($_SESSION['captcha']);
}
?>
[/php]
Can anyone assist in explaining how I add this to the code I have?
I also have this:
[php]<?php session_start(); ?>[/php]
Am I right in thinking this goes at the top of the page that has my php form code to make the captcha work?
Thanks in advance.