Advertisement
the-technoholik

reCAPTCHA PHP form handler

Feb 11th, 2015
324
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.82 KB | None | 0 0
  1. <?php
  2.   $resp = $_POST['g-recaptcha-response'];
  3.   $userIp = $_SERVER['REMOTE_ADDR'];
  4.   $secretKey = 'YOUR_PRIVATE_KEY';
  5.   $recaptchaUrl = 'https://www.google.com/recaptcha/api/siteverify'
  6.                       . '?secret='   . $secretKey
  7.                       . '&response=' . $resp
  8.                       . '&remoteip=' . $userIp;
  9.  
  10.   $ch = curl_init();
  11.   curl_setopt($ch, CURLOPT_URL, $recaptchaUrl);
  12.   curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  13.   $json = curl_exec($ch);
  14.   curl_close($ch);
  15.  
  16.   if (!$json) {
  17.     // TODO: Throw error due to the server (HTTP 5xx)
  18.   } else {
  19.       $arr = json_decode($json, true);
  20.       if(!$arr['success']) {
  21.           // TODO: User identified as a machine, display an error on the form page
  22.       } else {
  23.           // TODO: User identified as a human, continue processing...
  24.   }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement