In many cases, we want to identify where the form request raised by human, robots or spammers. For a secure application, we need to identify form request raised by human only. To enable this Google reCaptcha API to help us to do this checking process.
Now Google has been offered new reCaptcha API called Are you a robot? label input reCaptcha an entirely new idea captcha scheme. With the help of reCAPTCHA users can prove they are human without answering a CAPTCHA. Users need just do one click to verify they are not a robot. Using reCAPTCHA will protect our website from spammers or robots with better user performance. Google reCAPTCHA is very easy to integrate into a PHP script.
Step 1: Include required javascript and add the reCAPTCHA div tag on our web page.
Include the required API js link with proper reCaptcha placement tag information. It will help to generate and display the reCaptcha on our page.
1 2 3 4 5 |
<script src='https://www.google.com/recaptcha/api.js'></script> and <div class="g-recaptcha" data-sitekey="place_your_app_site_key_here"></div> |
Step 2: Add the HTML form with relevant fields and reCAPTCHA tag.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
<form action="" method="POST"> <div class="form-group"> <label for="full_name" class="required" >Name:</label> <input type="text" class="form-control" id="full_name" name="full_name" required /> </div> <div class="form-group"> <label for="email" class="required" >Email:</label> <input type="email" class="form-control" id="email" name="email" required /> </div> <div class="form-group"> <label for="message" class="required" >Message:</label> <textarea type="text" class="form-control" id="message" name="message" required ></textarea> </div> <div class="g-recaptcha" data-sitekey="place_your_app_site_key_here"></div> <div class="clearfix"></div> <div class="form-action-group"> <button type="submit" name="post_comment" class="btn btn-primary btn-form-action btn-submit">Submit</button> <button type="reset" class="btn btn-danger btn-form-action btn-reset">Clear</button> </div> </form> |
Step 3: PHP Script
Just add the write PHP script to validate the reCAPTCHA validation and get the response with JSON format and decode and store it a variable to use it in our application.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
<?php if(isset($_POST['post_comment'])){ if(isset($_POST['g-recaptcha-response']) && !empty($_POST['g-recaptcha-response'])){ $appSecretKey = 'place_your_app_secret_key_here'; $reCapcha = $_POST['g-recaptcha-response']; $verifyRequest = file_get_contents('https://www.google.com/recaptcha/api/siteverify?secret='.$appSecretKey.'&response='.$reCapcha); $response = json_decode($verifyRequest); if($response->success){ ?> <h4>Hi <?php echo $_POST['full_name']; ?>,</h4> <h2>"<?php echo $_POST['message']; ?>"</h2> <?php } else{ echo "reCaptcha Robot verification unsuccessful, Please try again."; } } else{ echo "You must click reCaptcha validation box."; } } ?> |