Spamming is one of the most common problem on the web. Every site owner wants to get rid of spamming whether it is spam traffic or spam messages or spam comments on your site.
To get rid of spamming you need to put some checks on your website. Previously people used to put some random number or string on the screen and verify it on the server side.
But this method is old days method. Google’s reCAPTCHA is a new way to verify a user. And it is very simple to verify the user. They just need a single click or tap to prove they are not a robot.
In this guide, you will learn how to integrate google reCAPTCHA on your website with PHP step by step.
You need to register your website and get an API key from google to use reCAPTCHA.
Register your website at – https://www.google.com/recaptcha/admin
Choose the type of captcha you want to use and the domain/domains for you will use this API key and click on the Register Button.
Now you will get your site key and secret key.
<script src='<a href="https://www.google.com/recaptcha/api.js">https://www.google.com/recaptcha/api.js</a>' async defer ></script>
Now add this HTML code where you want to show the reCAPTCHA widget.
<div class="g-recaptcha" data-sitekey="your_site_key"></div>
Replace your_site_key with the actual site key provided by the google.
Once you are done refresh your page and you will see the reCAPTCHA widget on your site.
You need to validate the response when a user click on the reCAPTCHA widget. To verify you need to write some PHP code as below
if(isset($_POST['g-recaptcha-response']) && !empty($_POST['g-recaptcha-response']))
$secret = 'your_actual_secret_key';
$verifyResponse = file_get_contents('https://www.google.com/recaptcha/api/siteverify?secret='.$secret.'&response='.$_POST['g-recaptcha-response']);
$responseData = json_decode($verifyResponse);
$succMsg = 'Your contact request have submitted successfully.';
$errMsg = 'Robot verification failed, please try again.';
Add your actual secret key in the above code and you are Done.
Feel free to write a comment if you face any problem. Will be happy to help you as soon as possible.
I am the owner of acmeextension. I am a passionate writter and reader. I like writting technical stuff and simplifying complex stuff.