Google’s New “I’m Not a Robot” reCaptcha Implemented in PHP

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 😀

5 thoughts on “Google’s New “I’m Not a Robot” reCaptcha Implemented in PHP”

  1. Anna

    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

    Reply ↓
    1. admin Post author

      Any code we post usually is created for internal purposes, then simplified for public reference. Glad to know it helped you out 🙂

      Reply ↓

Leave a Reply

Your email address will not be published. Required fields are marked *