Jump to content

New version of Google CAPTCHA and PHP


tsangaris

Recommended Posts

I am trying to implement the new version of captcha on my website.

What i did so far:

Inside the FORM:

echo '<div class="g-recaptcha" data-sitekey="XXXXXXXXXXXXXXXXXXXXXXXXXXXX"></div>';

Inside PHP:

$recaptcha = $_POST['g-recaptcha-response'];

if(!empty($recaptcha))
{

    $google_url = "https://www.google.com/recaptcha/api/siteverify";
    $secret = 'YYYYYYYYYYYYYYYYYYYYYYYYYYY';
    $ip = $_SERVER['REMOTE_ADDR'];
    $url = $google_url."?secret=".$secret."&response=".$recaptcha."&remoteip=".$ip;
    $res = getCurlData($url);
    $res = json_decode($res, true);


    if($res['success'] == 'false')
    {
       $captcha_error = "Please re-enter your reCAPTCHA.";
    }

}

The getCurlData function:

function getCurlData($url)
{

  $curl = curl_init();
  curl_setopt($curl, CURLOPT_URL, $url);
  curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
  curl_setopt($curl, CURLOPT_TIMEOUT, 10);
  curl_setopt($curl, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2.16) Gecko/20110319 Firefox/3.6.16");
  $curlData = curl_exec($curl);
  curl_close($curl);
  return $curlData;
}

What i want to achieve is to know when the no-Captcha box is checked. I want to throw an error to the user if he/she did not check that box.

So far i only throw an error if the response from Google is "We are not sure if you are human, please proceed to our second level of verification" [if($res['success'] == 'false')].

PS: most of the code is written by Srinivas Tamada. You can find it here.

Thanks in advance.

Link to comment
https://forums.phpfreaks.com/topic/293027-new-version-of-google-captcha-and-php/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.