TapeGun007 Posted February 9, 2017 Share Posted February 9, 2017 (edited) Here's my code: <?php include 'recaptchalib.php'; if(isset($_POST['g-recaptcha-response']) && !empty($_POST['g-recaptcha-response'])){ echo $_POST['g-recaptcha-response']."<p>"; $secret = "<secret key>"; $verifyResponse = file_get_contents('https://www.google.com/recaptcha/api/siteverify?secret='.$secret.'&response='.$_POST['g-recaptcha-response']); $responseData = json_decode($verifyResponse); echo "Response:".$verifyResponse."<p>"; if($responseData->success){ echo "Success"; }else{ echo "Failed"; } } ?> <!DOCTYPE HTML> <html> <head> <link rel="stylesheet" type="text/css" href="components/css/rs.css" /> <script src='https://www.google.com/recaptcha/api.js'></script> </head> <body> <form method="post" action="test2.php"> <input type="text" /> <div class="g-recaptcha" data-sitekey="<key>"></div> <input type="submit" /> </form> </body> </html> I've tried like 7 different websites coding example... my captcha fails every time. Their examples always work. What else could it be? Edited February 9, 2017 by TapeGun007 Quote Link to comment https://forums.phpfreaks.com/topic/303149-google-captcha/ Share on other sites More sharing options...
Jacques1 Posted February 9, 2017 Share Posted February 9, 2017 So what exactly “fails”? Be more specific. Officially, Google expects a POST request. You're sending a GET request (which may or may not work). Abusing the file_get_contents() function for HTTP requests is also problematic, because many systems disable this feature for security reasons. A much more robust solution would be to send a POST request with a proper HTTP library like cURL. Quote Link to comment https://forums.phpfreaks.com/topic/303149-google-captcha/#findComment-1542533 Share on other sites More sharing options...
TapeGun007 Posted February 9, 2017 Author Share Posted February 9, 2017 So when I run the code, the page loads... I check the captcha box and then submit... the echo "failed" is then triggered. I tried the following examples (and more): http://www.codexworld.com/new-google-recaptcha-with-php/ https://webdesign.tutsplus.com/tutorials/how-to-integrate-no-captcha-recaptcha-in-your-website--cms-23024 http://www.techflirt.com/integrate-google-no-captcha-recaptcha-in-php https://codeforgeek.com/2014/12/google-recaptcha-tutorial/ Quote Link to comment https://forums.phpfreaks.com/topic/303149-google-captcha/#findComment-1542536 Share on other sites More sharing options...
Jacques1 Posted February 9, 2017 Share Posted February 9, 2017 As I already said, use cURL. If there are still problems, turn your error reporting all the way up and post all errors/warnings/notices as well as the full response from the verification service. We're not sitting in front of your screen, so it's your job to provide relevant information. We cannot debug your code based on “It doesn't work”. In any case, copying and pasting random code from the Internet is definitely not a good idea. Most of it is outdated and/or poorly written. Either read the official documentation and write your own code. Or use a well-known library (I'm not sure if there is one for the new reCAPTCHA). Quote Link to comment https://forums.phpfreaks.com/topic/303149-google-captcha/#findComment-1542537 Share on other sites More sharing options...
TapeGun007 Posted February 10, 2017 Author Share Posted February 10, 2017 Hey Jacques1, I didn't really know much about cURL, so I had to read up and learn how it works. I used a var_dump to help figure out what I was doing as well. Bottom line is, you got me pointed in the right direction so thank you for that. Here is the code that works: $curl = curl_init(); curl_setopt_array($curl, [ CURLOPT_RETURNTRANSFER => 1, CURLOPT_URL => 'https://www.google.com/recaptcha/api/siteverify', CURLOPT_POST => 1, CURLOPT_POSTFIELDS => [ 'secret' => '***SECRET KEY HERE***', 'response' => $_POST['g-recaptcha-response'], ], ]); $response = json_decode(curl_exec($curl)); if($response->success){ echo "<p>Captcha was successful!</p>"; } var_dump($response); One curious question... is that my editor phpDesigner 8 gives me an error on the "curl_setopt_array" line with syntax error expected '[' Yet the code runs great. Is there something I am missing here? Quote Link to comment https://forums.phpfreaks.com/topic/303149-google-captcha/#findComment-1542539 Share on other sites More sharing options...
Jacques1 Posted February 10, 2017 Share Posted February 10, 2017 One curious question... is that my editor phpDesigner 8 gives me an error on the "curl_setopt_array" line with syntax error expected '[' If the editor is very old (pre PHP 5.4), then it might not recognize the short array syntax. Get an update or switch to a proper IDE like Netbeans or Eclipse. Quote Link to comment https://forums.phpfreaks.com/topic/303149-google-captcha/#findComment-1542540 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.