SugarClub
SugarClub
  • User
  • Site
  • Search
  • User
  • Groups & Discussions
    Groups & Discussions
    • Product Forums
      Product-focused Q&A, discussions, best practices, fixes, and help
      Product Forums
      • Sugar Market
      • Sugar Sell & Enterprise
      • Sugar Serve
      • sales-i
    • User Groups
      Professional, Industry, Language
    • Get Involved
      Learn how to become a Raving Fan
    • Social Club
      Live, interactive, virtual meetups with other Sugar customers and Sugar’s Subject Matter experts!
    • Leadership Lounge
      Network with fellow organizational leaders, ask questions, and share insights
    • Developers
      Visit DevClub, the SugarClub group for Sugar Developers
      Developers
      • DevClub
      • Mobile Developers
      • Developer Builds
        Supplemental access level required. Inquiries: developers@sugarcrm.com
    • Additional Groups (Access Required)
      Groups that require special access will be displayed here. Contact sugarclub@sugarcrm.com for assistance. Click here to see all groups
      Additional Groups (Access Required)
      • SugarCloud Platform
  • Product Information
    Product Information
    • Release Central
      Find release-specific content to prepare for your next Sugar update
    • Documentation & Resources
      Looking to expand your Sugar knowledge? Explore our in-depth documentation and other helpful resources!
    • Product Update Blogs
      Updates about each Sugar product
    • Customer Stories »
      Case Studies by SugarCRM
  • Training & Certification
    Training & Certification
    • Training & Certification Home
      Live & On-Demand classes, Quick Videos, Sugar Certifications, and more!
    • Quick Videos
      Short videos about using Sugar
    • My SugarU Dashboard »
    • SugarU News & Updates
  • Adoption
    Adoption
    • Grow Adoption Framework
      Get started on your adoption journey and review the adoption resources from SugarCRM
  • Calendar
  • News
    News
    • Sugar News
    • SugarCRM.com News »
    • Dev Blog
    • SugarCRM Marketplace Blog
  • Help
    Help
    • Welcome to Sugar!
      New to Sugar? Get started here!
    • SugarClub Help & Instructions
      Learn more about SugarClub and find answers to questions about this site
    • New to SugarClub?
      Start your community journey here
    • Technical Support
      Sugar's support resources
      Technical Support
      • Case Portal »
        Access the SugarCRM Case Portal
      • Working with Sugar Support »
        Find out more about engaging with the SugarCRM Support team
      • SugarCloud Information
        Find information about SugarCloud service updates and site status. Contact sugarclub@sugarcrm.com to request access
  • More from Sugar
    More from Sugar
    • DevClub
    • PartnerClub
    • Support
    • SugarOutfitters Marketplace
    • sugarcrm.com
  • DevClub
  • PartnerClub
  • Support
  • Marketplace
  • sugarcrm.com
DevClub
DevClub
Dev Tutorials Adding a google reCAPTCHA in a Web-to-Lead form
Click here to join this group and curate your SugarClub experience.
  • +On-Boarding Framework
  • +Customization Guides
  • +Modern UI Technical Guide
  • +Automated PHP Compatibility Tool
  • Did you know? Copying related records is a breeze!
  • How to write code for SugarCloud webinar Q&A
  • HOW TO: enforce ACL on Tags
  • Programatically manage Dropdown List
  • Remove custom fields created via package installation
  • Sugar Developer Tools
  • Tutorial:  How to register custom platforms in Sugar instances via Platform extension
  • Adding a google reCAPTCHA in a Web-to-Lead form
  • Sugar Developer Blog Style Guide

You are currently reviewing an older revision of this page.

  • History View current version

Adding a google reCAPTCHA in a Web-to-Lead form

The Web-To-Lead Form is placed on a public website where you generate leads from prospective customers who visit the site.

To avoid spam bots to enter leads in Sugar a common solution is to add a reCAPTCHA in the form. 

We can add custom code to the lead capture process by creating the file SugarRoot/leadCapture_override.php. 

However, due to the open defect #78741 the leadCapture_override.php is currently not being read.

As we need this file to implement the server side of the reCAPTCHA, first of all, we have to override the "WebToLeadCapture" entry point to use a custom file that will include the SugarRoot/leadCapture_override.php file. 

Step 1: Create the file SugarRoot/custom/Extension/application/Ext/EntryPointRegistry/WebToLeadCapture.php with the content: 

<?php
$entry_point_registry['WebToLeadCapture'] = array(
    'file' => 'custom/WebToLeadCaptureOverride.php',
    'auth' => false
);

Step 2: 

- Create the file SugarRoot/custom/WebToLeadCaptureOverride.php and copy all the content of the core file modules/Campaigns/WebToLeadCapture.php to inside that file 

Step 3: 

 

- Add the following lines to the file SugarRoot/custom/WebToLeadCaptureOverride.php just after the last require_once() and run a Quick Repair and Rebuild. 

//Added to include leadCapture_override.php if exists

if (file_exists('leadCapture_override.php')) {
     include('leadCapture_override.php');
}

At the time that the #78741 gets solved the steps above will not be needed and we can start with step 4.

Step 4:

Generate a new Web-to-Lead form

Step 5: 

 

Go through all the steps of the form generation as explained here and download the form.

Step 6:

 

Access https://www.google.com/recaptcha/, Navigate to My reCAPTCHA and register a new website. 

Choose the reCAPTCHA type, insert the domain where the form will be hosted and press Register. 

Step 7:

 

Open the Web-to-Lead file in a text editor and copy the lines in the Client Side integration into your web to lead form: 

Step 8: 

 

Create the SugarRoot/leadCapture_override file with the code below, replace the reCaptcha_secret with the value of your reCAPTCHA secret. 

<?php
// Checking reCAPTCHA

$recaptcha_site_secret ="Secret key";

if (isset($_POST) && !empty($_POST)) {

$captcha_response = htmlspecialchars($_POST['g-recaptcha-response']);
$curl = curl_init();
$captcha_verify_url = "https://www.google.com/recaptcha/api/siteverify";

        curl_setopt($curl, CURLOPT_URL,$captcha_verify_url);
        curl_setopt($curl, CURLOPT_POST, true);
        curl_setopt($curl, CURLOPT_POSTFIELDS, "secret=".$recaptcha_site_secret."&response=".$captcha_response);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);

        $captcha_output = curl_exec ($curl);
        curl_close ($curl);

        $decoded_captcha = json_decode($captcha_output);
        $captcha_status = $decoded_captcha->success;
        if($captcha_status === FALSE){
              //Landing page for when the Captcha fails
              header("Location: http://www.yourlandingpage.com/");
              exit();
        }
}
?>

Step 9 

Place your Web-To-Lead form on your website

Note: For Sugar Cloud installations a package will have to be created and uploaded via Module Loader. 

Check here how to create a package that copies files.