How to Use Google Recaptcha V2 In Php ?

Today now in this post i will show you How to Use Google Recaptcha V2 In Php ? This is a very simple article demonstrates of php google recaptcha v2 example. Here i will help you and give an example of how to add google captcha v2 in php registration form. Here we will look at an example of how to add the google recaptcha v2 in php. I will look at an example of google recaptcha v2 in php validation. Just need to follow the bellow step for google recaptcha v2 server side validation php.

As we also know, When you need to created the contact us form or any feedback form or any other form that access by publicly Then we need so many submission from the spam. They try to submit those form using curl request or something. So then you can not get the real user from those forms. Here, i will use google recaptcha v2 to prevent the spam submission. I will also create the contact us form and use google recaptcha v2 and prevent them. So on that way we need only real users and real feedback we will get.

Here i will give you the step of how we can implement google recaptcha v2 code in php. So let’s follow the below step to done this example:

Step 1: Add Google API Key

Now in this step i have to set the google site key and secret key. If we don’t have site key and secret key then we can create from here. At First need to click on this link : Recaptcha Admin

Ok, after the successfully register we can get site key and secret key from the like bellow preview.

Next, we need to copy the Site Key and Secret Key. you will use this keys on the next step:

Step 2: Create index.php File

In this step, copy bellow code and replace {GOOGLE-SITE-KEY} into Google Site Key replace there.

index.php

<html>
  <head>
    <title>PHP Google reCAPTHA V2 Example - codingspoint.com</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">
    <script src='https://www.google.com/recaptcha/api.js' async defer></script>
  </head>
  <body>
  
    <div class="container">
        <h1>PHP Google reCAPTHA V2 Example - codingspoint.com</h1>
  
        <form action="process.php" method="post">
            <div class="mb-3">
                <label for="exampleInputEmail1" class="form-label">Email address:</label>
                <input type="email" class="form-control" id="exampleInputEmail1" aria-describedby="emailHelp">
            </div>
  
            <div class="mb-3">
                <label class="form-label">Comment:</label>
                <textarea class="form-control"></textarea>
            </div>
  
            <div class="mb-3">
                <div class="g-recaptcha" data-sitekey="{GOOGLE-SITE-KEY}"></div>
            </div>
  
            <div class="mb-3">
                <button type="submit" class="btn btn-primary">Submit</button>
            </div>
  
      </form>
        
    </div>
  
  </body>
</html>

Step 3: Create process.php File

Now in this step, just need to copy bellow code and replace {GOOGLE-SECRET-KEY} into Google Secret Key replace there.

process.php

<?php
  
if($_SERVER["REQUEST_METHOD"] === "POST")
{
  
    $recaptcha_secret = "{GOOGLE-SECRET-KEY}";
    $response = file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=".$recaptcha_secret."&response=".$_POST['g-recaptcha-response']);
    $response = json_decode($response, true);
  
    if($response["success"] === true){
        echo "Form Submit Successfully.";
    }else{
        echo "You are a robot";
    }
  
}

Run PHP App:

All the need to steps have been done, now we need to type the given below command and then hit enter to run the PHP app:

php -S localhost:8000

Read Also: How To Implement Laravel 9 form validation ?

Thanks for read. I hope it help you. For more you can follow us on facebook

close

About Shahriar Sagor

My name is Shahriar sagor. I'm a developer. I live in Bangladesh and I love to write tutorials and tips that will help to other Developer's. I am a big fan of PHP, Javascript, JQuery, Laravel, Codeigniter, VueJS, AngularJS and Bootstrap from the early stage.

View all posts by Shahriar Sagor →