title: Google reCAPTCHA V2 checkbox HTML and PHP Contact Form Code
body: <p>I have used this site to find answers in the past and have decided to create an account, so I can share and help others. This post is not a question, but rather to share the code I have been using for a couple of company websites I created. We were receiving a lot of spam emails from our contact forms, so I implemented a &quot;Honey Pot&quot;, reCAPTCHA and other ways to eliminate the incoming spam. Below is my HTML code and PHP code for anyone who is struggling with this. So far I have not received any spam email since implementing this. All this code was created by information I found on this site, and other sites on the internet.</p>
  <p>HTML CODE: (The honeypot input &quot;website&quot; is hidden in CSS)</p>
  <pre><code>&lt;form id=&quot;form&quot; action=&quot;php/submit-en.php&quot; method=&quot;post&quot;&gt;
  
  &lt;div class=&quot;row&quot;&gt;
  
  &lt;div class=&quot;col-md-4&quot;&gt;
  
  &lt;div class=&quot;form-group&quot;&gt;
  
  &lt;label&gt;Name&lt;/label&gt;
  
  &lt;input class=&quot;form-control form-control-name&quot; name=&quot;the-name&quot; id=&quot;pccn&quot; autocomplete=&quot;off&quot; onpaste=&quot;return false&quot; onCopy=&quot;return false&quot; onCut=&quot;return false&quot; onDrag=&quot;return false&quot; onDrop=&quot;return false&quot; placeholder=&quot;Required&quot; type=&quot;text&quot; maxlength=&quot;31&quot; required&gt;
  
  &lt;script&gt;
  $(function() {
    $(&quot;#pccn&quot;).keyup(function() {
      console.log(this.value);
      this.value = this.value.replace(/(https?:\/\/)?([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w \.-]*)*\/?/mg, 'Links Not Accepted!');
    })
  });
  &lt;/script&gt;
  &lt;/div&gt;
  
  &lt;/div&gt;
  
  &lt;div class=&quot;col-md-4&quot;&gt;
  
  &lt;div class=&quot;form-group&quot;&gt;
  
  &lt;label&gt;Email&lt;/label&gt;
  
  &lt;input class=&quot;form-control form-control-email&quot; name=&quot;the-email&quot; id=&quot;pcemail&quot; autocomplete=&quot;off&quot; onpaste=&quot;return false&quot; onCopy=&quot;return false&quot; onCut=&quot;return false&quot; onDrag=&quot;return false&quot; onDrop=&quot;return false&quot; placeholder=&quot;Required&quot; type=&quot;email&quot; maxlength=&quot;32&quot; required&gt;
  
  &lt;/div&gt;
  
  &lt;/div&gt;
  
  
  &lt;div class=&quot;col-md-4&quot;&gt;
  
  &lt;div class=&quot;form-group&quot;&gt;
  
  &lt;label&gt;Subject&lt;/label&gt;
  
  &lt;input class=&quot;form-control form-control-subject&quot; name=&quot;the-subject&quot; id=&quot;pcsubject&quot; autocomplete=&quot;off&quot; onpaste=&quot;return false&quot; onCopy=&quot;return false&quot; onCut=&quot;return false&quot; onDrag=&quot;return false&quot; onDrop=&quot;return false&quot; placeholder=&quot;Optional&quot; maxlength=&quot;20&quot; type=&quot;text&quot;&gt;
  
  &lt;script&gt;
  $(function() {
    $(&quot;#pcsubject&quot;).keyup(function() {
      console.log(this.value);
      this.value = this.value.replace(/(https?:\/\/)?([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w \.-]*)*\/?/mg, 'Links Not Accepted!');
    })
  });
  &lt;/script&gt;
  &lt;label class=&quot;website&quot;&gt;
  &lt;input class=&quot;blind&quot; name=&quot;website&quot; type=&quot;text&quot; id=&quot;website&quot; placeholder=&quot;Website Link:&quot; tabindex=&quot;-1&quot; autocomplete=&quot;off&quot; onpaste=&quot;return false&quot; onCopy=&quot;return false&quot; onCut=&quot;return false&quot; onDrag=&quot;return false&quot; onDrop=&quot;return false&quot;/&gt;
  &lt;/label&gt;
  &lt;/div&gt;
  &lt;/div&gt;
  
  &lt;/div&gt;
  
  
  &lt;div class=&quot;form-group&quot;&gt;
  
  &lt;label&gt;Message&lt;/label&gt;
  
  &lt;textarea class=&quot;form-control form-control-message&quot; name=&quot;the-info&quot; id=&quot;pcinfo&quot; autocomplete=&quot;off&quot; onpaste=&quot;return false&quot; onCopy=&quot;return false&quot; onCut=&quot;return false&quot; onDrag=&quot;return false&quot; onDrop=&quot;return false&quot; placeholder=&quot;Required&quot; rows=&quot;10&quot; maxlength=&quot;2048&quot; required&gt;&lt;/textarea&gt;
  
  &lt;script&gt;
  $(function() {
    $(&quot;#pcinfo&quot;).keyup(function() {
      console.log(this.value);
      this.value = this.value.replace(/(https?:\/\/)?([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w \.-]*)*\/?/mg, 'Links Not Accepted!');
    })
  });
  &lt;/script&gt;
  &lt;div class=&quot;text-right&quot;&gt;
  &lt;br&gt;
  &lt;div class=&quot;g-recaptcha&quot; data-sitekey=&quot;YOUR SITE KEY&quot;&gt;&lt;/div&gt;
  &lt;button class=&quot;btn-primary&quot; data-callback='onSubmit' data-action='submit'&gt;Send Message&lt;/button&gt;
  &lt;/div&gt;
  
  &lt;/div&gt;
  &lt;/form&gt;
  </code></pre>
  <p>I added code to the HTML to reject any web links in the form, and copy/paste. These are not high use websites, so I am not too concerned with people having to manually input on the forms.</p>
  <p>PHP CODE: (submit-en.php)</p>
  <pre><code>&lt;?php
  if ( $_SERVER['REQUEST_METHOD']=='GET' &amp;&amp; realpath(__FILE__) == realpath( $_SERVER['SCRIPT_FILENAME'] ) ) {        
          header( 'HTTP/1.0 404 Page not found', TRUE, 404 );
          die( header('location: /no-spam.html') );
      }
  
  if(!isset($_GET)){
     header('Location: /no-spam.html');
  }
  
  if(isset($_POST['g-recaptcha-response'])){
            $captcha=$_POST['g-recaptcha-response'];
  }
  
  if(!$captcha){
            echo 'Please check the the captcha form.';
            exit;
  }
  
          $secretKey = &quot;YOUR SECRET KEY&quot;;
          $ip = $_SERVER['REMOTE_ADDR'];
          // post request to server
          $url = 'https://www.google.com/recaptcha/api/siteverify?secret=' . urlencode($secretKey) .  '&amp;response=' . urlencode($captcha);
          $response = file_get_contents($url);
          $responseKeys = json_decode($response,true);
          // should return JSON with success as true
          if($responseKeys[&quot;success&quot;]) {
                  echo 'Thanks for posting your message';
          } else {
          header('Location: /no-spam.html');
  } 
   
  // if the url field is empty, but the message field isn't
  if(isset($_POST['website']) &amp;&amp; $_POST['website'] == '' &amp;&amp; $_POST['the-info'] != ''){
  
  // CONTACT INFO
    $name = $_REQUEST['the-name'];
    $subject = $_REQUEST['the-subject'];
    $email = $_REQUEST['the-email'] ;
    $message = $_REQUEST['the-info'];
    $ip = $_SERVER['REMOTE_ADDR'];
    
  // ASSEMBLE HEADERS
    $ouremail = &quot;email@yourwebsite.com&quot;;
    $subject1 = &quot;Website Contact Form Submission&quot;;
    $subject2 = &quot;Website Submission Received&quot;;
    $headers = &quot;From: $ouremail\r\n&quot;;
    $headers .= &quot;Reply-To: 'email@yourwebsite.com'\r\n&quot;;
    $headers .= &quot;MIME-Version: 1.0&quot; . &quot;\r\n&quot;;
    $headers .= 'Content-type:text/html;charset=utf-8' . &quot;\r\n&quot;;
  
   // E-MAIL MESSAGE TO YOU
    $message = &quot;
  &lt;html&gt;
  &lt;head&gt;
  &lt;meta http-equiv=\&quot;Content-Type\&quot; content=\&quot;text/html; charset=UTF-8\&quot; /&gt;
  &lt;title&gt;Contact Form Submission&lt;/title&gt; 
  &lt;/head&gt; 
  &lt;body&gt;
    &lt;h3 style='color: #0D773C;'&gt;Website Submission:&lt;/h3&gt;
  &lt;p&gt;
  &lt;strong&gt;Name:&lt;/strong&gt;&amp;nbsp;  $name &lt;br&gt;
  &lt;strong&gt;Subject:&lt;/strong&gt;&amp;nbsp;  $subject &lt;br&gt;
  &lt;strong&gt;Email:&lt;/strong&gt;&amp;nbsp;&amp;nbsp;  $email &lt;br&gt;
  &lt;strong&gt;IP Address:&lt;/strong&gt;&amp;nbsp;&amp;nbsp;  $ip &lt;br&gt;
  &lt;/p&gt;
  &lt;p&gt;
  &lt;strong&gt;Message:&lt;/strong&gt;&lt;/p&gt;
  &lt;p style='font-style:italic';&gt;$message
  &lt;/p&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  &quot;;
  
    // SEND MAIL
  mail($ouremail,$subject1,$message,$headers);
   
  // E-MAIL MESSAGE TO CUSTOMER
    $message2 = &quot;
  &lt;html&gt;
  &lt;head&gt;
  &lt;meta http-equiv=\&quot;Content-Type\&quot; content=\&quot;text/html; charset=UTF-8\&quot; /&gt;
  &lt;title&gt;Contact Form Submission Received&lt;/title&gt; 
  &lt;/head&gt; 
  &lt;body&gt;
  &lt;h3 style='color: #0D773C;'&gt;Thank you for contacting us!&lt;/h3&gt;
  &lt;p style='font-style:italic';&gt;Thank you for contacting Your Company Inc.&lt;br&gt;
  We have received your message, and will get back to you as soon as possible.&lt;br&gt;
  If your message is of an urgent nature, please call us during normal business hours.&lt;/p&gt;
  &lt;p style='font-style:italic';&gt;
  Sincerely,&lt;br&gt;&lt;br&gt;
  Your Company Inc.&lt;br&gt;
  Tel: 123-456-7890&lt;/p&gt;
  &lt;p&gt;www.yourwebsite.com&lt;/p&gt;
  
  &lt;/body&gt;
  &lt;/html&gt;
  &quot;;
  
    // SEND MAIL
  mail($email,$subject2,$message2,$headers);
  
  header('Location: /thankyou.html');
  exit('Redirecting you to /thankyou.html');
  }
  </code></pre>
  <p>The first part of the PHP stops access to the submit-en.php file when access directly via web browser. The second part redirects to a No Spam page if it receives a GET request. The rest verifies if the captcha has been checked then verifies the response. Then it checks the if the honey pot was filled out. If so it will not send the email but redirect to the thankyou page so the spammer thinks the email went through. The remainder sends a nicely formatted email to you and a reply to the customer.</p>