Google reCAPTCHA v3 in PHP

Google has introduced reCAPTCHA v3 verification to prevent spamming and abusive terrific traffic on websites. In this tutorial, we’ll explain to you an easy way to integrate Google reCAPTCHA v3 in PHP.

Here, we will create a simple form to submit the name and email with validation of the reCAPTCHA v3 and store the information into the database. reCAPTCHA v3 doesn’t require the user friction.

Demo Example

Output - Google reCAPTCHA v3 in PHP - Clue Mediator
Output – Google reCAPTCHA v3 in PHP – Clue Mediator

Steps to add Google reCAPTCHA v3

  1. Register your website and generate keys
  2. Create HTML form
  3. PHP code for server side integration
  4. Output

File Structure

  • php-recaptcha-v3
    • index.php
    • recaptcha-validate.php

1. Register your website and generate keys

First of all, you need to register your website on Google reCAPTCHA for that click here and login with a Google account. After logged in, you will see the Google form as shown below.

Generate reCAPTCHA Keys – Google reCAPTCHA v3 in PHP – Clue Mediator
Generate reCAPTCHA Keys – Google reCAPTCHA v3 in PHP – Clue Mediator

Fill the information as mentioned in the above screenshot and click on the Submit button.

After successful registration, Google will provide you a SITE KEY and SECRET KEY which will be used to configure the form on the domains.

2. Create HTML form

Here, we will create a simple form with Google reCAPTCHA v3 that contains name and email to store in the database.

index.php

In the above code, you need to update <YOUR_SITE_KEY> in two places (Script URL & JavaScript Code).

3. PHP code for server side integration

Let’s create a table named user_data in the database.

Now, we will write a PHP code in the PHP file named recaptcha-validate.php to validate the reCAPTCHA and insert the data into the database.

recaptcha-validate.php

Don’t forget to update the <YOUR_SECRET_KEY> in the above code.

You will receive the following response from the Google reCAPTCHA API. Just print the $responseKeys variable in the above code to check the response.

For more information about server side integration click here.

4. Output

Run the web page and check the output in the browser.

Note: Google reCAPTCHA v3 is invisible so you won’t see a captcha on your website.

That’s it for today.
Thank you for reading. Happy Coding..!! 🙂

Demo & Source Code

GitHub Repository
If you found value in this article,
you can support us by buying me a coffee! ☕

You may also like...

Leave a Reply

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