php - Google's reCaptcha to reduce spam -
i noticed huge increase in spam emails coming website's contact form , looking using google's recaptcha method reduce and/or eliminate spam.
my website has contact form along .php file form. here snippet of html code contact form:
<form class='mailform' method="post" action="bat/rd-mailform.php"> <input type="hidden" name="form-type" value="contact"/> <fieldset> <label data-add-placeholder=""> <input type="text" name="name" placeholder="name" data-constraints="@lettersonly @notempty"/> </label>.............
i have registered google recaptcha obtain site key , secret key , have added <div>
within <form></form>
section. here snippet of code:
<div class="g-recaptcha" data-sitekey="mykey"></div>
i have added following head section:
<script src='https://www.google.com/recaptcha/api.js'></script>
since site has .php file contact form, how modify file recaptcha works properly?
here snippet of rd-mailform.php file running website:
<?php $recipients = 'email@gmail.com, email@verizon.net'; try { require './phpmailer/phpmailerautoload.php'; preg_match_all("/([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)/", $recipients, $addresses, preg_offset_capture); if (!count($addresses[0])) { die('mf001'); } if (preg_match('/^(127\.|192\.168\.)/', $_server['remote_addr'])) { die('mf002'); } $template = file_get_contents('rd-mailform.tpl'); if (isset($_post['form-type'])) { ....................
do need modify above .php file? online searching, looks may have include recaptchalib.php library file , upload hosting server.
no don't need library recaptcha sending information when handle form submission.
<?php $secretkey = "your secret key when register recaptcha"; if(!empty($_post['g-recaptcha-response'])){ $response = json_decode(file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=". $secretkey. "&response=" . $_post['g-recaptcha-response'])); if (!$response->success) { // there problem implement logic! } }
updated : // standard.html
<div id="recaptcha" class="g-recaptcha" data-lang="tr"></div> <script src="https://www.google.com/recaptcha/api.js?onload=onloadcallback&render=explicit" async defer></script> <script> grecaptcha.render('recaptcha', { 'sitekey' : 'yoursitekey', 'theme' : 'light' }); </script>
Comments
Post a Comment