Home Laravel How to implement Google new reCAPTCHA Code in PHP?

How to implement Google new reCAPTCHA Code in PHP?

by Shahriar Sagor

Today now in this post i will show you How to implement Google new reCAPTCHA Code in PHP? Sometimes We may need to use the Google captcha code on our registration form, contact us form etc in our application. Because the captcha code prevent the spams, bots etc. We know Google provide us to prevent the bots sending form the requests on that way we can protect our application.

So, now today i am going to give you a very simple example of how to add the google reCAPTCHA code in Core PHP from the scratch. Now in this example we need to generate our own API Site Key and the Secret from google account. But If we don’t know how to generate then no worry, here is all the step for generate API Key. Here i will create two files one is “index.php” file for generate the view for layout by using bootstrap css, another file one “process.php” for the check our google captcha is right or wrong. So, it is a really simple.

So, let’s start and follow the bellow step and after all this example we will find a layout as like as bellow, then we can customize as we want.

Generate Google Site Key and Secret

we need to generate a google site key and secret key. If we don’t have any site key and secret key then we need to create from here. At first click on this link : Recaptcha Admin

So after click we can see as like bellow view and we need register our site link this way:

implement Google new reCAPTCHA

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

implement Google new reCAPTCHA

Ok, now we need to copy our site key and then paste on index.php file and the secret key on file process.php , we can see where it’s place.

index.php

<html lang="en">
<head>
  <title>PHP - Google recaptcha code example</title>
  <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.js"></script>
  <script src='https://www.google.com/recaptcha/api.js'></script>
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
</head>
<body>


<div class="container">
  <div class="row">
        <div class="col-md-8 col-md-offset-2">
            <div class="panel panel-primary">
                <div class="panel-heading"><strong>Contact US Form</strong></div>
                <div class="panel-body">
                  <form action="process.php" class="form-horizontal" method="POST">


                    <div class="form-group">
                        <label class="col-md-4 control-label">Name:</label>
                        <div class="col-md-6">
                          <input type="text" class="form-control" name="name" placeholder="Enter Name" required="" />
                        </div>
                    </div>


                    <div class="form-group">
                        <label class="col-md-4 control-label">Email:</label>
                        <div class="col-md-6">
                          <input type="text" class="form-control" name="email" placeholder="Enter Email" required="" />
                        </div>
                    </div>


                    <div class="form-group">
                        <label class="col-md-4 control-label">Message:</label>
                        <div class="col-md-6">
                          <textarea type="text" class="form-control" name="message" placeholder="Enter Message" required=""></textarea>
                        </div>
                    </div>


                    <div class="form-group">
                        <label class="col-md-4 control-label">Captcha:</label>
                        <div class="col-md-6">
                          <div class="g-recaptcha" data-sitekey="Add Your Site Key"></div>
                    </div>


                    <div class="form-group">
                        <div class="col-md-6 col-md-offset-6" class="text-center">  
                          <br/>
                          <input class="btn btn-success" type="submit" name="submit" value="Submit">
                        </div>
                    </div>

                 
                  </form>
                </div>
            </div>
        </div>
  </div>
</div>


</body>
</html>

process.php

<?php


if($_SERVER["REQUEST_METHOD"] === "POST")
{


    $recaptcha_secret = "Add Your 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";
    }


}

Read Also: How to remove comma from string in jquery ?

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

close

You may also like