views:

2302

answers:

10

Hi

I am trying to add a reCAPTCHA to my site, but keep getting incorrect-captcha-sol error when I submit the answer.

Can anyone tell me if I am correct in doing the following?

I have a generic index.php, which includes contact.php. In contact.php I have inserted the following code:

require_once('recaptchalib.php');
$publickey = "XXXX";
$privatekey = "XXXX";

//the response from reCAPTCHA
$resp = null;
//the error code from reCAPTCHA, if any
$error = null;

if ($_POST['submit']) {
    $message = $_POST['message_txt'];
    $name = $_POST['name_txt'];
    $email = $_POST['email_txt'];

$emailBody = $message;
$to = 'xx';
$from = $name.' <'.$email.'>';
$subject = 'XX Website Enquiry';
$headers = 'From: '.$from; 

$resp = recaptcha_check_answer($privatekey, $_SERVER["REMOTE_ADDR"], $_POST["recaptcha_challenge_field"], $_POST["recaptcha_response_field"]);

if ($resp->is_valid) {
 echo 'captcha correct';
 if (mail($to,$subject,$emailBody,$headers)) {
  //echo 'mail sent';
  $confirmation = 'sent';
 }
 else {
  //echo 'mail not sent';
  $confirmation = 'error';
 }
} else {
 # set the error code so that we can display it. You could also use
 # die ("reCAPTCHA failed"), but using the error message is
 # more user friendly

 $error = $resp->error;

 echo $error;
}

}

In my html I inserted the CAPTCHA like this:

<form name="contactForm" method="post" action="index.php?id=contact&action=submit#contact">
    <tr><td>Name</td><td><div align="right">
      <input type="text" name="name_txt" class="input">
      </div></td></tr>
    <tr><td>Email</td><td><div align="right">
      <input type="text" name="email_txt" class="input">
    </div></td></tr>
    <tr><td height="10"></td></tr>
    <tr><td colspan="2">Message</td></tr>
    <tr><td colspan="2"><textarea name="message_txt" class="textarea" style="width:200px; height:100px"></textarea></td></tr>
    <tr><td colspan="2"><?php echo recaptcha_get_html($publickey, $error); ?></td></tr>
    <tr><td colspan="2" style="padding-top:10px;"><input type="image" src="images/header_06.gif" name="submit" value="submit"></td></tr>
    </form>

I cannot see that I am doing anything wrong, but would appreciate any constructive criticism.

TIA

A: 

if you are posting the check twice - eg once from javascript and once via php the second one will fail as the API only allows a solution to return valid once.

Hope that helps, Josh

Josh
A: 

Are you sure you are typing in correct words?

this one is from recaptcha website :

Line 1       "true" or "false". True if the reCAPTCHA was successful
Line 2  if Line 1 is false, then this string will be an error code. reCAPTCHA can 
    display the error to the user (through the error parameter of api.recaptcha.net/challenge).
     Implementations should not depend on error code names, 
as they may change in the future.


    Example: If your response looks like this:

    false
    **incorrect-captcha-sol**

    ... you can add '&error=incorrect-captcha-sol' to the challenge request URL, 
and the user will get an error code.
Wbdvlpr
+3  A: 

I have solved this, it is most of the unusual things I have come across, my syntax was previously:

<table>
<form>
<tr><td></td></tr>
</form>
</table>

I changed it to this:

<form>
<table>
<tr><td></td></tr>
</table>
</form>

Because of this switch, suddenly the recaptcha_response_field and recaptcha_challenge_field are posting values back to the form.

I cannot think why this because all MY form variables got posted back before the switch.

Thanks all for the pointers.

Anriëtte Combrink
+1  A: 

I can not believe it. The last comment about the table being changed is working. Ahhh... 2 hours of a headache for being before

shylor
Thanks, please 1+ the answer.
Anriëtte Combrink
+1  A: 

Wooooow. I can not believe it. although the syntax with the form inside the table is not completly valid according to xhtml, it is strange that something like that could be prob for recaptcha.... thx for the solution, i'd never found the solution without this hint.

martin
Thanks, please 1+ the answer.
Anriëtte Combrink
+1  A: 

That's because the form cannot be on the outside of the tr.....it has to be on the outisde of table.....form cannot be inserted into table, it can be inserted into the td though.

den
A: 

In my case, the error was to set the form without specifying:

method="POST"

Cheers!

esteve
A: 

The "table" answer solved my problem with an open source version of Coppermine Gallery (v 1.4.19) and the reCaptcha Plugin (version v2.7). Thank you!!

Liz
Can you give the answer a 1+, thanks.
Anriëtte Combrink
A: 

Wow! I can't believe it. I was having the EXACT same problem, but it was only occurring on my Mac with Firefox and Safari. Oddly enough, it had been working on IE6, 7, and 8 on Windows XP, Vista, and 7.

I moved the tag ahead of my opening tag that contained all the form fields and moved the closing tag after the closing tag and VOILA!

Talk about trivial! ;-)

Great find, Anriëtte -- and thanks for sharing!!

Brett
Hey, can you please 1+ the answers, thanks ;)
Anriëtte Combrink