0


I'm having an issue using Google captcha in may php contact form.
I followed the Google official guide and the code I wrote seems to be correct but I receive always the incorrect-captcha-sol error.

Client side code:

<div class="row">
    <div class="12u">
        <form id="mailform" method="post" action="website_mailer.php" novalidate="novalidate">
            <div>
                <div class="row half">
                    <div class="6u">
                        <input type="text" name="name" id="name" placeholder="Name" />
                    </div>
                    <div class="6u">
                        <input type="text" name="email" id="email" placeholder="Email" />
                    </div>
                </div>
                <div class="row half">
                    <div class="12u">
                        <input type="text" name="subject" id="subject" placeholder="Subject" />
                        <input type="text" class="myantispam" name="leaveblank">
                        <input type="text" class="myantispam" name="dontchange" value="http://" >
                    </div>
                </div>
                <div class="row half">
                    <div class="12u">
                        <textarea name="message" id="message" placeholder="Message"></textarea>
                    </div>
                </div>
                <div class="row half">
                    <div class="4u">
                        <div class="g-recaptcha" data-sitekey="xxxx"></div>
                    </div>
                    <div class="8u">
                        <a href="#" class="button form-button-submit">Send Message</a>
                        <a href="#" class="button button-alt form-button-reset">Clear Form</a>
                    </div>
                </div>
            </div>
        </form>
    </div>
</div>

Server side code (PHP):

<?php
require_once('recaptchalib.php');

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

if (!$resp->is_valid) {
        die ("The reCAPTCHA wasn't entered correctly. Go back and try it again." .
         "(reCAPTCHA said: " . $resp->error . ")");
    } else {
      /* proper code */
    }
?>

Can you suggest me a solution? I'm not able to find a solution.

Thanks!

neoben
  • 743
  • 12
  • 30

2 Answers2

3

I solved the issue using the following code

$response=json_decode(file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=SECRET_KEY_HERE&response=".$captcha."&remoteip=".$_SERVER['REMOTE_ADDR']), true);
if($response['success'] == false){
    //Captcha incorrect
}
else {
    //Success code here
}

instead of

<?php
require_once('recaptchalib.php');

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

if (!$resp->is_valid) {
        die ("The reCAPTCHA wasn't entered correctly. Go back and try it again." .
         "(reCAPTCHA said: " . $resp->error . ")");
    } else {
      /* proper code */
    }
?>

Thanks to Locke Donohoe who suggested the answer here.

Community
  • 1
  • 1
neoben
  • 743
  • 12
  • 30
0

in root directory from the script create php.ini file with this:

allow_url_fopen = On

rok
  • 47
  • 6