Yogesh Chauhan's Blog

How to add recaptcha version 3 to PHP website?

in PHP on March 19, 2021

You are going to need recaptcha keys to get started on this. Get it from Google here.

Step 1: JavaScript

Add this JavaScript code between your head tags using script tag or include a seperate .js file in head tags.



grecaptcha.ready(function () {
  grecaptcha
    .execute("YOUR_RECAPTCHA_SITE_KEY", { action: "contact" })
    .then(function (token) {
      var recaptchaResponse = document.getElementById("recaptchaResponse");
      recaptchaResponse.value = token;
    });
});


Step 2: HTML form

Add this line after your submit button but before the form tag ends.



 <form>
    ...
    ...
   <button type="submit">Submit</button>
   <input type="hidden" name="recaptcha_response" id="recaptchaResponse">
 </form>


Step 3: PHP

Use this code for server side integration.



<?php // Check if form was submitted:
if ($_SERVER['REQUEST_METHOD'] === 'POST' &#038;&#038; isset($_POST['recaptcha_response'])) {

    // Build POST request:
    $recaptcha_url = 'https://www.google.com/recaptcha/api/siteverify';
    $recaptcha_secret = 'YOUR_RECAPTCHA_SECRET_KEY';
    $recaptcha_response = $_POST['recaptcha_response'];

    // Make and decode POST request:
    $recaptcha = file_get_contents($recaptcha_url . '?secret=' . $recaptcha_secret . '&#038;response=' . $recaptcha_response);
    $recaptcha = json_decode($recaptcha);

    // Take action based on the score returned:
    if ($recaptcha->score >= 0.5) {
        // Verified - send email
    } else {
        // Not verified - show form error
    }

} ?>


Credit: stevencotterill. Google has nice guides for client side and server side integration as well.


Most Read

#1 How to check if radio button is checked or not using JavaScript? #2 Solution to “TypeError: ‘x’ is not iterable” in Angular 9 #3 How to add Read More Read Less Button using JavaScript? #4 How to uninstall Cocoapods from the Mac OS? #5 PHP Login System using PDO Part 1: Create User Registration Page #6 How to Use SQL MAX() Function with Dates?

Recently Posted

#Aug 15 Is PHP still good for back-end programming? #Aug 10 How to create a multisite network in WordPress? #Aug 3 How to create a circle that follows a cursor using JavaScript and CSS? #Aug 3 How to make a curtain slider using jQuery and CSS? #Aug 2 How to progressively load images and add a blurry placeholder? #Aug 1 How to create a placeholder loader (throbber) using CSS?
You might also like these
How to add Local State to a Class in React?ReactThe flex-grow, flex-shrink and flex-basis Properties in CSSCSSAngular 9 time clock update every minute, second, hourAngularHow to load variables, functions, and mixins from another module in SCSS?SCSSCSS backface-visibility PropertyCSSHow to create a Recent Posts function in WordPress?WordPressA list of wp-cli commands to use via SSHWordPressWhat is a “promise” in JavaScript?JavaScriptHow to set the permissions on all files and folders in Mac?MiscellaneousHow to Hide HTML elements automatically after few seconds using JavaScript?HTMLHow to swap images on hover using CSS?CSSHow to link/add CSS file to HTML Document?CSS