How to add recaptcha version 3 to PHP website?

March 19, 2021

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

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

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

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

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

Step 3: 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 uninstall Cocoapods from the Mac OS? #4 How to Use SQL MAX() Function with Dates? #5 How to add Read More Read Less Button using JavaScript? #6 PHP Login System using PDO Part 1: Create User Registration Page

Recently Posted

Apr 7 Solution for “Yarn build: Failed because of a stylelint error” Apr 7 Make sure your links have descriptive text Apr 7 How to add Laravel to WordPress using Sage theme (and install Tailwind CSS)? Apr 2 How to create a sidebar using pure CSS? Apr 1 How to switch dark and light themes using pure CSS? Apr 1 How to calculate elapsed time in JavaScript?

You might also like these

How to refresh a page using PHP at regularly occurring intervals?PHPPHP Variables ScopePHPHow to Use ROLLUP Operator in SQL and MySQL?SQL/MySQLHow to create a cross-browser smooth scrolling with jQuery?jQueryHow to get the height and width of an element using JavaScript?JavaScriptIs monitoring an employee’s work on a computer a desirable or undesirable activity?MiscWordPress: How to find all posts from a custom post type with specific custom field?WordPressHow to create two segues with two UIButtons on a single page (Swift 5.0)?SwiftWhat is HTMLUnknownElement?HTMLA Quick Guide to Object-Oriented Programming in PHPPHPSome SQL LIKE Operators We Need to Keep in MindSQL/MySQLLIMIT and OFFSET in PostgresPostgres