DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Why Google Data Migration Gets Stuck at 99%: Causes and Proven Fixes
  • Run Gemma 4 on Your Laptop: A Hands-On Guide to Google's Latest Open Multimodal LLM
  • The Death of "Text-Only" ChatOps: Why Google's A2UI Matters for DevOps and SRE
  • Boosting React.js Development Productivity With Google Code Assist

Trending

  • A Walk-Through of the DZone Article Editor
  • How AI Is Rewriting Full-Stack Java Systems: Practical Patterns with Spring Boot, Kafka and WebSockets
  • Engineering LLMOps: Building Robust CI/CD Pipelines for LLM Applications on Google Cloud
  • Retesting Best Practices for Agile Teams: A Quick Guide to Bug Fix Verification
  1. DZone
  2. Coding
  3. Languages
  4. How To Add Google reCAPTCHA v3 In PHP Contact Form

How To Add Google reCAPTCHA v3 In PHP Contact Form

We will see how to integrate Google reCaptcha v3 in php to protect your web application from a spambot. Google reCaptcha v3 API most popular captcha solution.

By 
Manas Singh user avatar
Manas Singh
·
Nov. 24, 20 · Tutorial
Likes (3)
Comment
Save
Tweet
Share
20.6K Views

Join the DZone community and get the full member experience.

Join For Free

Google has introduced another and upgraded form of recaptcha called Google reCAPTCHA v3. It gives greater protection from the spam bot or maltreatment in your web structures or web forms. Google reCAPTCHA v3 API works on the premise of spam score which implies that the reCAPTCHA v3 API restores the spam score of each input given by the client action.

Benefits of Google reCAPTCHA v3

This reCAPTCHA v3 is exceptionally simple to utilize as compared to Google reCAPTCHA v2 on the grounds that the client doesn't have to click on the checkbox which is in the Google reCAPTCHA v2. It just ascertains the spam score dependent on the information and client's movement and chooses whether it is a spam action or not.

Let's See How To Integrate Google reCAPTCHA v3 in PHP Application

In this instructional tutorial, we will see to add Google reCAPTCHA v3 in PHP with the assistance of a contact form. On the other hand, if you have any contact form or some other form like login, signup etc on your site and you are stressed over the spam assault, at that point you are at the ideal spot. We encourage you to peruse this instructional exercise till the end and you will get clear information to shield your forms from spambot attack.

Here I am listing out all the steps which will use in this tutorial to implement Google reCAPTCHA v3 in PHP.

  1. Create Site key and Secret Key from Google reCAPTCHA Admin console.
  2. Create a simple contact form in PHP
  3. Create a PHP file to validate the form using Google reCAPTCHA V3 API

Create Site key and Secret Key From Google reCAPTCHA Admin console

The first step is to generate the site key and secret key for google reCaptcha API. To do so, you need to login into Google reCAPTCHA Admin console and add your site to Google reCaptcha admin console and generate the site key and secret key. It's very simple. However, you can follow the detailed instruction to generate Google reCaptcha site key and secret key here.

After generating the keys, please keep it handy for further use in our application.

Create a Simple Contact Form In PHP

Now we will create a simple contact form in PHP to demonstrate the implementation of Google reCaptcha v3 in PHP.

So let's dive into the code.

HTML
 




x
22


 
1
<script async src="https://www.google.com/recaptcha/api.js?render=<YOUR_SITE_KEY>"></script>
2

          
3
<input type="text" class="form-control" id="name" placeholder="Enter your name" name="name" required>
4

          
5
<input type="text" class="form-control" id="email" placeholder="Enter your email" name="email" required>
6

          
7
<textarea name="comment" class="form-control" id="comment" placeholder="Enter your comment" required></textarea>
8

          
9
<input type="hidden" name="recaptcha_response" value="" id="recaptchaResponse">
10

          
11
<input type="submit" name="submit" value="Submit" class="btn btn-success btn-lg">
12

          
13
<script>
14

          
15
grecaptcha.ready(function () {
16

          
17
               grecaptcha.execute('<YOUR_SITE_KEY>', { action: 'submit' }).then(function (token) {
18

          
19
                   var recaptchaResponse = document.getElementById('recaptchaResponse');
20

          
21
                   recaptchaResponse.value = token;
22
</script>   



Here replace 'YOUR_SITE_KEY' with your generated site key. There are no other changes required in it.

Create a PHP file to validate the form using Google reCAPTCHA V3 API

Now we will create a simple PHP file to validate the form input value and call the Google reCaptcha v3 API to calculate the spam score. So let's check the code below:

PHP
 




xxxxxxxxxx
1
40


 
1
<?php
2

          
3
// Do some basic form validation and value sanitization and then process the value below.
4

          
5
if(isset($_POST['name']) && $_POST['name']!="" && isset($_POST['email']) && $_POST['email']!="")
6

          
7
{
8

          
9
// This is Google reCapctha v3 API url. It will use secret key to validate the user request.
10

          
11
   $google_recaptcha_url = 'https://www.google.com/recaptcha/api/siteverify';
12

          
13
   $recaptcha_secret_key = '<YOUR_SECRET_KEY>'; // Replace with your generated Secret key
14

          
15
  $set_recaptcha_response = $_POST['recaptcha_response'];
16

          
17
   // Make the request and get the response by making below request.
18

          
19
   $get_recaptcha_response = file_get_contents($google_recaptcha_url . '?secret=' . $recaptcha_secret_key . 
20

          
21
'&response=' . $set_recaptcha_response);
22

          
23
   $get_recaptcha_response = json_decode($get_recaptcha_response);
24

          
25
   // Based on the spam score, take your action
26

          
27
   if ($get_recaptcha_response->success == true && $get_recaptcha_response->score >= 0.5 && 
28

          
29
$get_recaptcha_response->action == 'submit') {
30

          
31
       $success_msg = "You can process your application flow.";
32

          
33
   } else {
34

          
35
       $err_msg = "Something went wrong. Please try again after sometime.";
36
   }
37

          
38
}
39

          
40
?>


Please note once you call the Google reCaptcha API, you will get the response on below format:

Plain Text
 




xxxxxxxxxx
1


 
1
{ "success": true, "challenge_ts": "2020-11-24T15:31:26Z", "hostname": "localhost", "score": 0.95, "action": "submit" }


So the based on the 'Score ' value you can process your form or application flow.

That's all about the implementation of Google ReCaptcha in v3. I personally recommend to use it to prevent any kind of abuse.

PHP Google (verb) Form (document) Contacts (Apple)

Published at DZone with permission of Manas Singh. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Why Google Data Migration Gets Stuck at 99%: Causes and Proven Fixes
  • Run Gemma 4 on Your Laptop: A Hands-On Guide to Google's Latest Open Multimodal LLM
  • The Death of "Text-Only" ChatOps: Why Google's A2UI Matters for DevOps and SRE
  • Boosting React.js Development Productivity With Google Code Assist

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook