How To Instal Google reCaptcha On The Website

Hi guys, in this article I will make a tutorial on how to install reCaptcha and make buttons so they can't be pressed before the visitor confirms that he is not a robot. This reCaptcha function is very useful to prevent robots from crawling to the last link because the crawler robot cannot pass through reCaptcha.

Mounting Method

So to install we will put the PHP code on the server which means if you are a using Blogspot as CMS you need Hosting. You can search for free hosting on the internet, and please note that this tutorial cannot be practiced on localhost because the domain must be registered with Google first.


After that, we will add a few lines of Javascript code and one line of Div tags Element in the HTML section. But before everything starts we have to register our domain with Google reCaptcha, it's easy and I will explain all the tutorials in the following section.


How to register the domain with Google reCaptcha

1. Register your domain by opening the following link https://www.google.com/recaptcha/admin
2. Add a new label and fill it according to the required data.
3. Click the "Register" button then you will be taken to a new page, you will get 2 key codes, namely the Site Key and Secret Key. This is what we will use for the next stage.
4. After the registration / domain registration stage is complete, it's time for us to implement it on your site. Open the site page you want to reCaptcha and edit it in HTML mode.



How to instal Google reCaptcha on you site
1. Add the following Javascript code line, you can add it in the <head> section or it can only be on the page. The JS code doesn't need to be edited, leave it like that, you just have to copy and paste it.

<script src="https://www.google.com/recaptcha/api.js"></script>
<script>
document.getElementById("button1").disabled = true;
function enableBtn(){document.getElementById("button1").disabled = false;}
</script>

2. Find the code </form> (and make sure the form that creates the button), above it put the following code.

<div class="g-recaptcha" data-sitekey="FILL WITH THE SITE KEY CODE GIVEN GOOGLE reCAPTCHA" data-callback="enableBtn"></div>

3. In the form code section, look for the button type code, then add the following ID id="button1". The example in question

<form action="/example.php" method="post" enctype="multipart/form-data">
<input class="button" type="submit" name="submit" value="Get Link" id="button1">
<div class="g-recaptcha" data-sitekey="FILL WITH THE SITE KEY CODE GIVEN GOOGLE reCAPTCHA" data-callback="enableBtn"></div>
</form>

8. Now let's go to the editing part of the PHP file it's example.php, on the hosting you have, look for the PHP file that executes the input form. The example in the form code is like on step 3, the php file is named example.php.

Look at the following PHP code, make the position of the old PHP code sandwiched with the new PHP code, as follows.


<?php
$captcha     = $_POST['g-recaptcha-response'];
if  (!$captcha) {
    echo $error;
} 
else {
    $response = file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=FILL WITH THE SITE KEY CODE GIVEN GOOGLE reCAPTCHA
&response=".$captcha."&remoteip=".$_SERVER['REMOTE_ADDR']);
    if ($response.success != true) { "You are a robot";} 
    else {echo 'Congrat! You are not spammer.';}
}
?>


9. And the tutorial is finished.

How do you do it, guys? This tutorial will be easy if you have experience editing codes, especially PHP and HTML. And it will be even easier if you are familiar with the file configuration on the hosting that you have.

Comments

Popular posts from this blog

How to Use External WebCam Camera on Windows Laptop

4 Reason Why The Ads is Not Showing On Google Admob

Complete Tutorial on How to Edit Videos With Kinemaster