Menu
How to use google recaptcha in PHP

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

Google, PHP By Dec 05, 2019 No Comments

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">
  <head>
    <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="https://getbootstrap.com/docs/3.3/dist/css/bootstrap.min.css" rel="stylesheet">
  </head>
  <body>
<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">
            <label>Name*</label>
            <input type="text" id="Name" name="Name" class="form-control" placeholder="Enter Name">
        </div>
        <div class="form-group">
            <label>Phone*</label>
            <input type="text" id="Phone" name="Phone" class="form-control" placeholder="Enter Phone">
        </div>
        <div class="form-group">
            <label>City</label>
            <input type="text" id="City" name="City" class="form-control" placeholder="Enter City">
        </div>
        <button class="btn btn-lg btn-primary btn-block" type="submit" id="submit" name="submit">Send Enquiry</button>
      </form>

    </div> <!-- /container -->
  </body>
</html>

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="https://www.google.com/recaptcha/api.js" 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.

<?php
    if(isset($_POST["submit"])){
        $captcha=$_POST["g-recaptcha-response"];
        $secretkey="6Lc1qMIUAAAAAJPTxLb-BgmzfaKIZwPYeT2TZTKs";
        $url="https://www.google.com/recaptcha/api/siteverify?secret=".urlencode($secretkey)."&response=".urlencode($captcha)." ";
        $response=file_get_contents($url);
        $responsekey=  json_decode($response,TRUE);
        if($responsekey["success"]){
            echo "Send mail";
        }
        else
        {
            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 ~

 

Author

Hello i am Vivek Moyal PHP developer and writes tutorials for students and beginners. Love to explore new cities

No Comments

Leave a comment

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