Why Captcha verification is required?
Captcha Verification is used to prevent automated programs submitting spam or unwanted contents. It is used to verify the data is entered by human or bots and prevent the bots content. So we can avoid spam data’s.
Steps Invloved in Captcha verification:
Captcha works in live domains only, it won’t work in local host, we can add more than one domains for creating site key and secret key.
There are 3 main and easy steps involved in captcha verification, which are listed below,
- Create site key and secret key from https://www.google.com/recaptcha link.
- Now we have to include that captcha field in form.
- Server side Validation of that captcha and submitting details.
Site key is used for displaying captcha in front end,secret key is used for validation of captcha at server side.
How to include captcha in form:
<div class="col-md-6 col-sm-6 col-xs-12"> <div class="g-recaptcha"data-sitekey="6Lft23kUAAAAAFlpwYZle9ZjF4GaJRlRMF93m7L3"></div> </div>
In this site key use your site key generated at step1.and include below mentioned script file, for the visibility of the captcha.
<script src=’https://www.google.com/recaptcha/api.js’></script>
Now u can see that captcha is included in your form, now u have to validate that captcha and need to submit the form,
Validation and Submitting Details:
Upto my knowledge, server side validation only used for Google captcha verifications,here,i suggested server side validation for captcha as mentioned below,
Here we have to use that secret key for server validation.
First we have to confirm captcha verification,whether captcha is checked or not,and images selected correct or not.
g-recaptcha-response is the captcha response sent by GET or POST method,here am using post method.
First if we did’t get get g-recaptcha-response it goes to else part and throws the validation error.
Once we get the response it goes inside the condition and checks the accuracy of entered captacha,using api and inbuilt functions google captcha,
if the captcha is verified successfully,we can get the other details via post method and after validation we can send a mail to the mentioned user using php mail() function or SMTP method.
if(isset($_POST['g-recaptcha-response']) && !empty($_POST['g-recaptcha-response'])) { $captcha=$_POST['g-recaptcha-response']; if(!$captcha) { $msg = ‘’; } else { $secretKey = "6Lft23kUAAAAAG8JraxzPUea1C2KSmAo2nH-Hm4w"; $ip = $_SERVER['REMOTE_ADDR']; $ch = curl_init(); curl_setopt($ch,CURLOPT_URL,"https://www.google.com/recaptcha/api/siteverify"); curl_setopt($ch,CURLOPT_POST, 1); curl_setopt($ch,CURLOPT_POSTFIELDS,"secret=".$secretKey."&response=".$captcha."&remoteip=".$ip); curl_setopt($ch,CURLOPT_RETURNTRANSFER, true); $response = curl_exec($ch); $err = curl_error($ch); curl_close($ch); $responseKeys = json_decode($response,true); if(intval($responseKeys["success"]) == 1) { //here u can get the details of form and send to mentioned mail id. } else { echo "<script>alert('Please Enter valid details');window.location='contact.html';</script>"; } } else { echo "<script>alert('Please Enter Valid Captacha');window.location='contact.html';</script>"; } } } else { echo "<script>alert('Please verify captcha'); window.location='contact.html';</script>"; }