PHP

PHP CONTACT FORM WITH GOOGLE RECAPTCHA

For local testing, also register "localhost", "127.0.0.1", or "::1".

reCaptcha will provide 2 keys - Site key and secret key.

RECAPTCHA REGISTRATION

01

PUT CAPTCHA IN FORM <form method="post">   <input type="text" name="name"/>    <input type="email" name="email"/>    <textarea name="msg"></textarea>

HTML CONTACT FORM

02

  <div class="g-recaptcha"          data-sitekey="SITE-KEY">     </div>     <input type="submit" value="Go!"/>  </form>

LOAD RECAPTCHA LIBRARY <script src="https://www.google.com/ recaptcha/api.js"></script> 

PASTE SECRET KEY $url = "https://www.google.com/ recaptcha/api/siteverify?secret= SECRET-KEY&response=". $_POST["g-recaptcha-response"];   RECAPTCHA VERIFY  $verify = json_decode (file_get_contents($url));

PHP FORM PROCESSING (A)

03

PHP FORM PROCESSING (B)

04

PROCEED IF OK if ($verify->success) {   $to = "jon@doe.com";   $sub = "CONTACT";   $body = "";   foreach ($_POST as $k=>$v)   { $body .= "$k: $v\r\n";  }   mail($to, $sub, $body); } else { echo "Invalid captcha"; }