I had been using ReCaptcha V1 for a number of years with no real problem. Figuring out how the heck to get V2 running became a nightmare. As much as anyone would like to think it's well documented, IT'S NOT unless you happen to be a master coder. These days I scrape by on coding, so having a very clear, simple and straight forward example would be wonderful. I found a lot of examples of people using Curl and other fun ways to implement this. All of those examples ended up with the stupid recaptcha always being
SUCCESS : FALSE and I still don't understand why. Anyway, I do now having it working and hopefully someone can benefit from these short example files I'm attaching. I certainly don't claim to be a great coder, but this actually does work consistently.
I happen to use sessions in my forms but code still works the same with or without session enabled.
Here's the HTML (again, if you don't want sessions, eliminate the PHP line at the top and call your file cap_test.html). I got this code from codeforgeek so I definitely thank them so much!!
CAP_TEST.PHP
<?php session_start(); ?>
<html>
<head>
<title>Google recapcha demo - Codeforgeek</title>
<script src='https://www.google.com/recaptcha/api.js'></script>
</head>
<body>
<h1>Google reCAPTHA V2 Demo</h1>
<form id="comment_form" action="cap_process.php" method="post">
<input type="email" placeholder="Type your email" size="40"><br><br>
<textarea name="comment" rows="8" cols="39"></textarea><br><br>
<div class="g-recaptcha" data-sitekey="enter your site key here"></div>
<input type="submit" name="submit" value="Post comment">
</form>
</body>
</html>
CAP_PROCESS.PHP
<?php
session_start();
$secret = "enter your secret captcha code here";
$email;$comment;$captcha;
if(isset($_POST['email'])) { $email=$_POST['email']; }
if(isset($_POST['comment'])) { $email=$_POST['comment']; }
if(isset($_POST['g-recaptcha-response'])) { $captcha=$_POST['g-recaptcha-response']; }
// If !$captcha is true, that says the user hit the submit without doing the captcha
if (!$captcha) {
echo '<h2>Please check the the captcha form.</h2>';
exit;
}
// This gets the response and checks it for being true. If it is it prints thanks for posting the comment and exits.
$response = file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=$secret&response=".$captcha."&remoteip=".$_SERVER['REMOTE_ADDR']);
if($response.'success'==true) { echo '<h2>Thanks for posting comment.</h2>'; }
?>
So this is fun for testing. In my real form, I don't really care whether they tried to bypass the captcha or if it failed for some reason, I'm still going to make them try again, so this is a small bit of the actual code I use in my PHP processing script:
// Set errorflag variable to false so we'll assume this will run with no errors
$_SESSION["errorflag"] = "false";
// Now go check if the recaptcha was entered correctly
// If $_POST['g-recaptcha-response'] is not set, that means the user hit submit without completing the recapcha and we'll send it back
if(isset($_POST['g-recaptcha-response'])){ $captcha=$_POST['g-recaptcha-response']; }
// So if $captcha was set, lets go see if it was successfull by getting the response and putting the answer into $response
if($captcha){$response=file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=$secret&response=".$captcha."&remoteip=".$_SERVER['REMOTE_ADDR']);}
// So now if either the $captcha was not set OR we did not get a success from recaptcha
// we'll set the error flag to indicate that recaptcha was bad
// and load the sanitized form data into session variables and go on back to the form
if ((!$captcha) or ($response."success"==false)) {
$_SESSION["applocation"] = $applocation;
$_SESSION["firstname"] = $firstname;