Advertisement
Guest User

Google reCAPTCHA Example Code

a guest
Dec 4th, 2014
4,940
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.82 KB | None | 0 0
  1. Say 'goodbye' to old Captcha technology. Google launches new "No CAPTCHA reCAPTCHA" to check if you're a robot or human.
  2.  
  3. Read more at http://thn.li/ipix
  4.  
  5. <?php
  6. $db = mysqli_connect("localhost","root","password","database_name");
  7.  
  8. function getresponse($url)
  9. {
  10. $curl = curl_init();
  11. curl_setopt($curl, CURLOPT_URL, $url);
  12. curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
  13. curl_setopt($curl, CURLOPT_TIMEOUT, 15);
  14. 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");
  15. $curlData = curl_exec($curl);
  16. curl_close($curl);
  17. return $curlData;
  18. }
  19.  
  20. $msg='';
  21. if($_SERVER["REQUEST_METHOD"] == "POST")
  22. { $recaptcha=$_POST['g-recaptcha-response'];
  23.     if(!empty($recaptcha))
  24.     {
  25.         $google_url="https://www.google.com/recaptcha/api/siteverify";
  26.         $secret='Inout Google Secret Key';
  27.         $ip=$_SERVER['REMOTE_ADDR'];
  28.         $url=$google_url."?secret=".$secret."&response=".$recaptcha."&remoteip=".$ip;
  29.         $res=getresponse($url);
  30.         $res= json_decode($res, true);
  31.         if($res['success'])
  32.             {
  33.             //Get Username , Password from Database
  34.             //Match and Check verify login details
  35.             //Create Session
  36.             }
  37.         else
  38.             {
  39.             $msg="Sorry. Re-enter your reCAPTCHA.";
  40.             }
  41.  
  42.     }
  43.    
  44.     else
  45.     {
  46.         $msg="Sorry. Re-enter your reCAPTCHA.";
  47.     }
  48.  
  49. }
  50. ?>
  51.  
  52. <!doctype html>
  53. <html lang="en">
  54.     <head>
  55.         <meta charset="UTF-8" />
  56.         <title>Google reCaptcha 2</title>
  57.         <script src="https://www.google.com/recaptcha/api.js"></script>
  58.     </head>
  59. <body>
  60.     <form action="" method="post">
  61.     Username <input type="text" name="username" class="input" />
  62.     Password <input type="password" name="password" class="input" />
  63.     <div class="g-recaptcha" data-sitekey="Insert Google Site Key"></div>
  64.     <input type="submit"  value="Log In" />
  65.     <span class='msg'><?php echo $msg; ?></span>
  66.     </form>
  67. </body>
  68. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement