All are the best.

Wednesday, 6 July 2011

Adding captcha with web-to-lead forms

There was a big problem for adding captcha in sf  web to lead forms but I did.

Here I put some code to help anyone who is looking for it.

you should have 2 files:

The first file is a web page file that you put the web to lead form that you have made it before in salesforce.
The second one is the php file to check the captcha code and then send data to sf leads.

Note: 
1- For this captcha you should signup for "publickey" and "privatekey" from here
2- Download the recaptchalib.php from here
3- The server (host) should support cUrl php library

First file code (contactus.php)


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Form</title>
</head>
<body>
<!-- Your form must be put instead of this form and put the bold parts in that form  -->
<form action="check-this-first.php" method="post"  name="emailForm" id="test" style="padding: 0px; margin: 0px;" >
<input type=hidden name="oid" value="00D200000008By">
<input type=hidden name="retURL" value="your return url like www.yahoo.com">
  <table>
  <tr>
      <td >Title: <br />
      <input  style="width:200px;" id="title" maxlength="5" name="title" size="7" type="text"/>
        </td>
    </tr>
     <tr>
      <td >First Name: <br />
      <input  style="width:200px;" id="first_name" maxlength="40" name="first_name" size="20" type="text"/>
        </td>
    </tr>
    <tr>
      <td >Last Name: <br />
      <input  style="width:200px;" id="last_name" maxlength="80" name="last_name" size="20" type="text" />
        </td>
    </tr>
    <tr>
      <td >Email:<br />
        <input  style="width:200px;" id="email" maxlength="80" name="email" size="20" type="text" /></td>
    </tr>
  <tr>
      <td >Country: <br />
        <input  style="width:200px;" id="country" maxlength="40" name="country" size="20" type="text" />
       </td>
    </tr>
    <tr>
      <td >City: <br />
        <input  style="width:200px;" id="city" maxlength="40" name="city" size="20" type="text" />
       </td>
    </tr>
     <tr>
      <td >Company:<br />
      <input  style="width:200px;" id="company" maxlength="40" name="company" size="20" type="text" />
        </td>
    </tr>
     <tr>
      <td >Mobile: <br />
        <input  style="width:200px;" id="mobile" maxlength="40" name="mobile" size="20" type="text" />
       </td>
    </tr>
  <tr>
      <td >Description: <br />
       <textarea name="description"></textarea>
       </td>
    </tr>
     <tr>
      <td align="left" >
      <span class="style1 style2">Verify Code:<br />
      <br />
        <!-- Captcha//-->
        <?php
          require_once('recaptcha/recaptchalib.php');
          $publickey = "***************"; // you got this from the signup page
          echo recaptcha_get_html($publickey);
        ?>
        <!-- Captcha //-->
  </td>
    </tr>
   <tr>
    <td>
      <br /><br />
      <input type="submit" name="submit">
         </td>
    </tr>
  </table>
</form>
</body>
</html>

Second file (check-this-first.php)


<?php

session_start();
ob_start();
  require_once('recaptcha/recaptchalib.php');// download and put in the recaptcha folder 
          //Recaptcha Settings
       $publickey = "********************"; // you got this from the signup page
  $privatekey = "********************";//your private key

//curl method posting
//extract data from the post
      extract($_POST);
  if ($submit){
  $ok = 1;
  $resp = recaptcha_check_answer ($privatekey,
                                $_SERVER["REMOTE_ADDR"],
                                $_POST["recaptcha_challenge_field"],
                                $_POST["recaptcha_response_field"]);
  if (!$resp->is_valid) {
 $ok = 0;
}
  if ($ok){
//set POST variables
$url = 'https://www.salesforce.com/servlet/servlet.WebToLead?encoding=UTF-8';
$fields = array(
'oid'=>urlencode($oid),
'retURL'=>urlencode($retURL),
'title'=>urlencode($title),
'first_name'=>urlencode($first_name),
'last_name'=>urlencode($last_name),
'email'=>urlencode($email),
'country'=>urlencode($country),
'city'=>urlencode($city),
'company'=>urlencode($company),
'mobile'=>urlencode($mobile),
'description'=>urlencode($description)

);
  //url-ify the data for the POST
foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
rtrim($fields_string,'&');
//print_r($fields_string);
  //open connection
$ch = curl_init();
  //set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_POST,count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS,$fields_string);
//execute post
$result = curl_exec($ch);
//close connection
curl_close($ch);
 echo 'Congratulation the leads has been sent successfully.';
} //if ok
else {
 echo "<h4>Sorry - Invalid Captcha  Please try again  </h4>";
}
 } //if submit.
 ?>