PHP Contact Form with Google reCAPTCHA

Captcha code is a combination of some readable text with some distorted shapes to be read only by human being to confirm that the user is not a bot.The Google reCAPTCHA protects your application from spam with the better user experience.In this tutorial, we have share how to integrate the Google reCAPTCHA in PHP contact form.
the reCAPTCHA is a free CAPTCHA service that protects websites from spam and abuse. This is a PHP library that wraps up the server-side verification step required to process responses from the reCAPTCHA service. This client supports both v2 and v3. This is a very simple script, you can copy paste and modify according to your requirement.
Downlod Google reCAPTCHA code library click here.
Register the domain of your website at Google reCAPTCHA Admin console.
  • Label – choose label name.
  • reCAPTCHA type – Select reCAPTCHA v2 >> I’m not a robot Checkbox
  • Domains – Specify the domain of your website.
Get Site Key and Secret Key:
  • Site Key : This key is used in the HTML code of the reCAPTCHA widget.
  • Secret Key : This key helps to authorize communication between your site and the reCAPTCHA server.
Before started to implement the Integrate the Google reCAPTCHA in PHP, look files structure:
  • integrate-the-google-recaptcha-in-php
    • css
      • style.css
    • recaptcha
      • src
        • autoload.php
    • config.php
    • index.php
    • contact.php
include reCAPTCHA Widget to HTML Form
Add tag g-recaptcha tag element in the HTML form where you want to display the reCAPTCHA widget.
  • The g-recaptcha DIV element has a class (named g-recaptcha") and data-sitekey attributes.
  • The Site Key of the reCAPTCHA API will be specified in the data-sitekey attribute.
Step 1: Create config.php
Step 2: Create HTML Form
Step 3: Ajax code with with custom validation (include index.php)
>
Step 4: Verify reCAPTCHA Response (using Server-side Validation)
Step 5: Create header (header.php)
This view contains the header section of the webpage. The Bootstrap library is used to provide a better UI, so, include it in the header section.
Located at the top of a website, the header typically contains elements that include a company’s logo, website navigation, and contact information.
Step 6: Create a view file named footer.php
This view contains the footer section of the webpage.
  • jQuery – needed by Bootstrap JavaScript.
  • Bootstrap JavaScript – to make cool UI