I just saw the announcement for Google’s revised reCAPTHCA API, so I decided threw together a quick example. It worked well without even looking at the documentation. So simple to integrate it’s ridiculous. Here is the PHP (no security for simplicity)…
if ( ! empty($_POST)) { $q = http_build_query(array( 'secret' => 'YOUR_SECRET_KEY', 'response' => $_POST['g-recaptcha-response'], 'remoteip' => $_SERVER['REMOTE_ADDR'], )); $result = json_decode(file_get_contents('https://www.google.com/recaptcha/api/siteverify?'.$q)); if ($result->success) { // Continue processing form data } }
And the html…
<script src='https://www.google.com/recaptcha/api.js'></script> <form method="post"> <div> <label for="input-name">Name</label> <input type="text" name="name" id="input-name" value="<?php echo htmlentities($_POST['name']); ?>"> </div> <div> <label for="input-email">Email</label> <input type="email" name="email" id="input-email" value="<?php echo htmlentities($_POST['email']); ?>"> </div> <div> <label for="input-msg">Message</label> <textarea name="msg" id="input-msg"><?php echo htmlentities($_POST['email']); ?></textarea> </div> <div class="g-recaptcha" data-sitekey="YOUR_SITE_KEY"></div> <div> <input type="submit" value="Send"> </div> </form>
That’s really all there is to it.
Kudos to Google for making CAPTCHA’s less of a PITA.
Happy coding 😀
I’m thinking about implementing this as a drop-in replacement for OpenCart’s CAPTCHA. Think anyone would be interested?
How to reload Captcha with ajax success or error return?
That ventures into where I make my living charging for services 😉 See https://developers.google.com/recaptcha/docs/display
Thank you for this article.
I was looking for an information about how to implement reCaptcha ” I’m not a robot ” checkbox into PHP form.
I have checked few scripts but none of them worked.
Your script works perfectly.
Thank you so much
Anna
Any code we post usually is created for internal purposes, then simplified for public reference. Glad to know it helped you out 🙂