Inserting php captcha code into existing form?

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 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); } } ?>


& here’s the captcha code:


<?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


Session CAPTCHA: {$_SESSION['captcha']}
Form CAPTCHA: $request_captcha
HTML; unset($_SESSION['captcha']); } ?>


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.

In my opinion you should go to the horse’s mouth: for there are links on how to incorporate it in your website. That’s what I did, it is pretty straight forward in my opinion.

Ok thanks, had a look at that link and it was no help at all - apart from the code naturally being different, their own help section for where to add their code is as vague as I’ve ever seen:

“look for the code that generates that error message.” and “if your form sends mail, search for the code that does that.” Ok, er, & then do what!?

Does anyone know how I can just simply implement the code in my original post? I can add it in various places ‘as is’ but it either doesn’t have any affect, throws up a blank page on submit or an error message.

Anyone else have any ideas on how to do this?
I’m guessing this is really simple - if you have a better understanding of php!

Sorry, this doesn’t help me either - getting a captcha to work on it’s own isn’t the problem - it’s how to integrate it into an existing form I can’t work out.

You don’t need to integrate it with the form. Just check if the captcha is ok, if not then don’t process the form.

I don’t know what you mean by don’t process the form. Everything has to be connected (form & captcha) to work, yes?

One last time before I give up, any more ideas or solutions please? Someone must be willing to share their wisdom!

Sponsor our Newsletter | Privacy Policy | Terms of Service