How To Use Google reCAPTCHA in PHP | Best Way to Stop Bots

By | December 5, 2019
How to use google recaptcha in PHP

In this tutorial you will lean How to use reCaptcha in PHP. Using Captcha in our form helps us to stop spam entries. We can stop the spammers from filling our the webforms. When we create a page for getting the enquiry from customer or any details our mail box is flooded with spam mails along with our genuine mails. Sometimes some spammers start the relat process and it ends with some serous problems like our hosting provider susoends our account or our IP gets balcklisted. To overcame from this sutuation we use CAPTCHA. You can find many scrips over the internet but best and easy to use is reCAPTCHA. Lets start how we can use reCaptcha in our PHP code.

You can follow the below instruction with code or watch the Video.

First of all we will create a from with some fields. This form is having 3 fields and a button.

<html lang="en">
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta name="description" content="">
    <meta name="author" content="">
    <title>Demo Form</title>
    <link href="" rel="stylesheet">
<div class="container">

        <form class="form-signin" action="<?php echo $_SERVER["PHP_SELF"]; ?>" method="post">
        <h2 class="form-signin-heading">Demo Form</h2>
        <div class="form-group">
            <input type="text" id="Name" name="Name" class="form-control" placeholder="Enter Name">
        <div class="form-group">
            <input type="text" id="Phone" name="Phone" class="form-control" placeholder="Enter Phone">
        <div class="form-group">
            <input type="text" id="City" name="City" class="form-control" placeholder="Enter City">
        <button class="btn btn-lg btn-primary btn-block" type="submit" id="submit" name="submit">Send Enquiry</button>

    </div> <!-- /container -->

As you can see we have a from. Now open the browser and search for reCaptcha you can find it at first place in Google. Click over it now follow the below instructions.

  • Now click over the admin Console which is at the top right corner
  • Now click on the plus sign which is for adding a new website.
  • Here i will use localhost. Enter the name as localhost
  • There are 2 types we can use but we will use the v2 select it. As v3 is having some problems. Now choose the challenge but most of the websites use “i am not a robot” option.
  • Accept the terms and conditions and click on the submit.
  • Now you can see 2 keys. Site key and Secret Key.
  • For using the site key we will click over the client side integration link and copy the script line and div tag of recaptcha and paste it at our form.

Script Line

<script src="" async defer></script>

Div Tag

<div class="g-recaptcha" data-sitekey=""></div>

Use the above line in your form and enter the data-sitekey.

As you can see we have completed the setting up the form now we will use this recaptcha in our PHP code for checking the requests. For this use the below code please do not forget to add the secret key.

        $url="".urlencode($secretkey)."&response=".urlencode($captcha)." ";
        $responsekey=  json_decode($response,TRUE);
            echo "Send mail";
            echo "stop";

In if else condition you can use your code. Hope you like the tutorial please share it with others. If you have any advise or have anything to update the script please let me know. Please comment below

~ Thank you for reading happy coding ~


Leave a Reply

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