Many of you asked for a tutorial on implementing a Captcha to the Bootstrap forms; here it is. I chose a popular Captcha solution, Google's ReCaptcha, for verification. Show In the tutorial, I will be using a working HTML contact form from my previous tutorial. Our form will be using HTML5 sprinkled with some Bootstrap scaffolding and a JavaScript validator. We will submit it with AJAX (the page will not reload) and process the form values with PHP. Finally, we will send an email with PHP and return a response to the original page that will be shown in a status message above the form. I will mostly focus on working with Captcha today, so if you have missed my last tutorial, have at least a quick look at it. Explore the demo or download the files we will be working with. And we can start right now. Note: I updated this tutorial to Bootstrap 4 already but no worries if you are still using Bootstrap 3 in your project. Bootstrap 3 version is also part of the download. Demo and linksDemo Download files Demo Bootstrap 3 Register your siteTo use ReCaptcha, you will need to register your website on ReCaptcha's website first. After successful registration, you will get a pair of keys to use with your ReCaptcha. Leave the page open or copy the keys to a text file; we will need them soon. We will use the contact form's template from the previous tutorial, + we will add a reCAPTCHA element and a hidden input next to it to help us with the JavaScript validation. Pay attention to these changes:
PHPIn PHP, we will be using Google's client library that will take care of the verification. You can use Composer to install it in your project, download it from GitHub or use the version I included in the download package. Again, pay attention to these changes:
The script then composes the email message, sends it, and returns a JSON response. (The script is submitted by AJAX in default)
JavaScriptThere will be a few changes in our JavaScript. As mentioned before, we will add two callbacks for the ReCAPTCHA and change the value of our hidden input The second change to my previous tutorial will be resetting the ReCaptcha after the form's
submission. We will do this by calling the
ResultThis is it. You should have a working contact Bootstrap contact form with ReCaptcha and PHP background now. I hope you have enjoyed the tutorial. Check out my other Bootstrap tutorials, my Bootstrap freebies, or useful Bootstrap snippets. FAQsHow do I add "I am not a robot" in HTML?The most convenient way is to add Google ReCaptcha. Google ReCaptcha is now available as a free, easy-to-use service that helps you prevent spam and abuse. What is Google ReCaptcha?ReCaptcha is a free service from Google that helps you prevent spam and abuse on your website. For example, if you have a contact form on your site, spammers may use it to send junk emails. With ReCaptcha, you can verify that the request is coming from a human, not a computer by asking the individual to solve a simple math problem or check a box next to the distorted text. Google has released several versions of ReCaptcha over the years: Classic ReCaptcha - launched in 2008, this version of the code is still widely used today and supported in all major browsers Invisible ReCaptcha - launched in 2013 as an alternative to classic ReCaptcha for websites that want greater flexibility in how they display the code on their pages. Invisible ReCaptcha also allows users to continue interacting with your site without interruptions while they solve challenges. This version only works with modern browsers (Chrome 39+, IE11+). What is CAPTCHA?CAPTCHA stands for "Completely Automated Public Turing test to tell Computers and Humans Apart." This is one of many ways we use to verify that you are human. reCAPTCHA uses images that are easy for humans but difficult for computers to recognize, so bots fail while humans succeed. Is reCAPTCHA a bot?reCAPTCHA is not a bot. It uses artificial intelligence and machine learning to distinguish between humans and bots, which means it can identify real people and bots. What does reCAPTCHA do?reCAPTCHA helps Google protect its users from spam, abuse, and other malicious activities. reCAPTCHA challenges users to verify that they are human by asking them to complete a simple task that computers often have trouble with, like identifying street signs or separating items based on their shapes. Google uses this technology to improve its products and services. How do I create a CAPTCHA in HTML and CSS?Note: Captcha can protect From some Automated attacks as well. Example: First, create a section for Captcha with HTML. The below code will generate a design for a captcha and from the CSS file we will add style and on the image(refresh) click, we will generate a new captcha by calling generate() method from JavaScript.
How do I add reCAPTCHA v2 in HTML?Generate reCAPTCHA Keys. Set your Site's Name in the Label field.. Choose the reCAPTCHA v2 "Im not a robot" Checkbox from the reCAPTCHA type.. Add your domain name.. Accept the reCAPTCHA Terms of Service.. Click on the SUBMIT button.. How do I add a CAPTCHA to my website?How to Use reCAPTCHA. Sign up for an API key pair for your site. Click here to start the process.. Select 'Admin Console'. Type your website URL.. Select reCAPTCHA v2.. Enter your website's URL under 'Domains'. Enter emails of the administrators.. Accept reCAPTCHA Terms of Service.. Submit the form. Your API keys will be generated.. How do I create a CAPTCHA code?In the Google Cloud console, go to the reCAPTCHA Enterprise page. Go to reCAPTCHA Enterprise.. Verify that the name of your project appears in the resource selector at the top of the page. ... . Click Create key.. In the Display name field, enter a display name for the key.. Create a site key for a website or mobile platform.. |