Header Ads Widget

header ads

How to add reCAPTCHA in any website

 In this article, you will learn how to add reCAPTCHA in any website. Follow the instruction given below step by step.

OUTPUT:
form with captcha

******************************************

Step 1 : Design your form as you want for any language such as basic HTML, PHP or ASP.NET.
eg.-

simple form design



Step 2 : add <div id="ReCaptchContainer"></div> where you want to place the captcha.

Step 3 : add following script just above closing of body tag (</body>):
__________________________________________________

    <script src="https://www.google.com/recaptcha/api.js?           onload=renderRecaptcha&render=explicit" async defer></script>

            <script type="text/javascript">
                var your_site_key='place your site key here';
                var renderRecaptcha=function(){
                grecaptcha.render('ReCaptchContainer',{
                'sitekey':'place your site key here',
                'callback':reCaptchaCallback,
                theme:'light',
                type:'image',
                size:'normal'
                });
                };
                var reCaptchaCallback= function (response){
                if(response !==''){
                document.getElementById('lblMessage1').innerHTML="";
                }
                };                

            </script>
______________________________________________

Step 4 : Go to reCAPTCHA (google.com) 

Step 5 : type anything on lablel box.

Step 6 : Select reCAPTCHA v2

Step 7 : Select  "I'm not a robot" Checkbox

Step 8 : type localhost or your website url in domain field.

Step 9 : Check on Accept the reCAPTCHA Terms of Service

Step 10 : you can check or unckeck Send alerts to owners

Step 11 : Click on Submit.

Step 12 : Copy your site key and secret key and place the on your code.

form with captcha



Now you can run the project and you will get CAPTCHA on your website. If you want to check that the captcha is filled by user or not than you can follow the tutorial given bellow for Asp.net C# user:


Step 13: Generate OnClick event on button and go to .cs code.

Step 14: Add following namespaces:

using Newtonsoft.Json.Linq;
using System.Configuration;
using System.Net;
using System.IO;

Step 15: Add this code to check the captcha is solved or not:

public bool IsReCaptchValid()
        {
            var result = false;
            var captchaResponse = Request.Form["g-recaptcha-response"];
            var secretKey = "6LcMnswdAAAAAINJpQ30okbIkVijCJIxnVVVDO-K";
            var apiUrl = "https://www.google.com/recaptcha/api/siteverify?secret={0}&response={1}";
            var requestUri = string.Format(apiUrl, secretKey, captchaResponse);
            var request = (HttpWebRequest)WebRequest.Create(requestUri);
            using (WebResponse response = request.GetResponse())
            {
                using (StreamReader stream=new StreamReader(response.GetResponseStream()))
                {
                    JObject jResponse = JObject.Parse(stream.ReadToEnd());
                    var isSuccess = jResponse.Value<bool>("success");
                    result = (isSuccess) ? true : false;
                }
            }
            return result;
        }


Step 16 : Place following code in button_click event:

            if (IsReCaptchValid())
            {
                lblMessage1.Text = "valid"; //or your code
            }
            else
            {
                lblMessage1.Text = "Invalid"; //or your code
            }

Now you can run the project and you will get output like this:

captcha validation

follow my YouTube video for better understanding:https://youtu.be/pVQQBMSPoEs


Thanks.



Post a Comment

0 Comments