Before start you have to generate or collect recaptcha SITE_KEY and SECRET_KEY from admin console at https://www.google.com/recaptcha/
Three easy steps.
Step One
Add following scripts before </head> tag.
<!--Recaptcha script--> <script src='https://www.google.com/recaptcha/api.js'></script> <!-- Recaptcha field Required--> <script> window.onload = function() { var recaptcha = document.forms["cvform"]["g-recaptcha-response"]; recaptcha.required = true; recaptcha.oninvalid = function(e) { // do something alert("Please complete the captcha"); } } </script>
Step Two
Add following markup to the form where you want to show recaptcha.
<div class="g-recaptcha" data-sitekey="YOUR_RECAPCHA_SITE_KEY"> </div>
Step Three
Add following Recaptcha verification code snippet to the PHP script.
//reCAPTCHA validation if(isset($_POST['g-recaptcha-response']) && !empty($_POST['g-recaptcha-response'])) { $secret = 'YOUR_RECAPTCHA_SECRET_KEY'; $verifyResponse = file_get_contents('https://www.google.com/recaptcha/api/siteverify?secret='.$secret.'&response='.$_POST['g-recaptcha-response']); $responseData = json_decode($verifyResponse); if($responseData->success) { $succMsg = 'Your contact request have submitted successfully.'; } else { $errMsg = 'Robot verification failed, please try again.'; } }