Captcha code is one of the best and easy way to protect your online form from automated abuse and spam. But writing code to create captcha is not easy. It take some time to make captcha code for website and also if you want to add some extra functionality to your captcha like add voice/sound to your captcha it really take allot of time and we don’t want to waste or time to make this. Google help us to solve it google provide free captcha services for your website Named as Google reCatcha.
Google reCaptcha is very easy to use you need to add reCaptcha library to your project download the google reCaptcha from here after download you need to create your public and private key to use google recaptcha you need to login to your google account and then go to this link https://www.google.com/recaptcha
Now add your site link where you want to add google recaptcha widget as shown in above image.
after adding your site and you get your public and private key as shown in above image.
Code to call google widget to form:
<?php $key = '6LeGce8SAAAAAGU3307E86QKFyRsoR25F7lkKKHS'; // Get this key when you sign up for recaptcha echo recaptcha_get_html($key); ?>
code to verify captcha
<?php if($_SERVER['REQUEST_METHOD'] == 'POST'){ // Get data post from the forms $name = $_POST['name']; $privateKey = ''; // Add your private key here $recaptchaVerify = recaptcha_check_answer ($privateKey, $_SERVER["REMOTE_ADDR"], $_POST["recaptcha_challenge_field"], $_POST["recaptcha_response_field"]); // Some default fields // Validating code if (!$resp->is_valid) { echo "Catcha error: ". $recaptchaVerify->error; // When error occur } else { echo 'Welcome'. $_POST['name'].'<br>'; echo 'Done!! You enter valid Captcha code.'; } } ?>
As in above code you need to add your public key to get google recaptcha to your website. And at next page where you verify your captcha code you need to enter your private key to work this captcha and to function
recaptcha_check_answer('private key', 'Client Ip', $_POST["recaptcha_challenge_field"], $_POST["recaptcha_response_field"]);
after this we check that user add correct captcha code or not using “$proccessVar->is_valid”
This is the simple way to integrate the google recaptcha to your web forms.
Happy Coading!.