4

after several trying (Explicitly render), I finally show the reCaptcha in my website.

Inside <head></head> tag..

<script src="https://www.google.com/recaptcha/api.js" async defer></script>

and inside my form

<form action="" method="post">
    <input placeholder="Enter Your Name" type="text" required/>
    <input placeholder="Enter Your Email" type="email" required/>
    <input placeholder="Enter Your Password" type="password" required/>
    <div class="g-recaptcha" data-sitekey="My_reCaptcha_Site_Key"></div>
    <?php echo $msg; ?>
    <input type="submit" value="Register">
</form>

and my php code is

<?php
$msg='';
if($_SERVER["REQUEST_METHOD"] == "POST"){
    $recaptcha=$_POST['g-recaptcha-response'];
    if(!empty($recaptcha)){
        include 'getCurlData.php';
        $google_url="https://www.google.com/recaptcha/api/siteverify";
        $secret='My_reCaptcha_Secret_Key';
        $ip=$_SERVER['REMOTE_ADDR'];
        $url=$google_url."?secret=".$secret."&response=".$recaptcha."&remoteip=".$ip;
        $res=getCurlData($url);
        $res= json_decode($res, true);
        //reCaptcha success check 
        if($res['success']){
            $msg="Your reCAPTCHA succeeded.";
        } else {
            $msg="Please re-enter your reCAPTCHA 1.";
        }
    } else {
        $msg="Please re-enter your reCAPTCHA 2.";
    }
}
?>

and my getCurlData.php code is like

<?php
function getCurlData($url){
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_TIMEOUT, 120);
$curlData = curl_exec($curl);
curl_close($curl);
return $curlData;
}
?>

I have given you the entire details which I've tried. But the problem is, reCaptcha not succeeded. Means, it is showing the tick mark but the $msg showing "Please re-enter your reCAPTCHA 1." I am unable to find the error.

For your information, I am testing it in localhost, and if succeeded, I will upload it in my GoDaddy hosting site.

I have found this procedure, but not worked.

Update:

As per Stretch, I tried

$res=file_get_contents($url);

instead of

getCurlData($url);

And the problem solved. However I am putting my question open.

So is there no need of curl ? Why should we use curl in reCaptcha ?

Community
  • 1
  • 1
Raja
  • 772
  • 1
  • 15
  • 38
  • 1
    Instead of `getCurlData()` try `file_get_contents()` and see what happens. – Just Lucky Really Apr 27 '15 at 21:27
  • file_get_contents() will only work if allow_url_fopen is set to 1 in your php ini file. Often you cannot change the php ini file on your live server because you are on shared hosting. In that case you can rely on Curl. – bart Jun 14 '15 at 22:59

1 Answers1

2

The reason it's not working is because you're trying to access HTTPS. The solution you shouldn't use is this:

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

The solution you should use is this:

curl_setopt($ch, CURLOPT_CAINFO, 'cacert.pem');

https://stackoverflow.com/a/316732/364841 has a link to the latest cacert.pem.

Community
  • 1
  • 1
basiphobe
  • 590
  • 6
  • 12