Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.3k views
in Technique[技术] by (71.8m points)

php - Google reCAPTCHA - keep getting `incorrect-captcha-sol`

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 help.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I have solved this, it is one of the most 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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...