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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.