$response = file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=".$secret."&response=".$response); $response = json_decode($response, true); if($response["success"] === true){ // actions if successful }else{ // actions if failed }
Tag Archives: captcha
Multiple Google ReCaptcha on a simple page
HTML
<form>
<h1>Form 1</h1>
<div><input type="text" name="field1" placeholder="field1"></div>
<div><input type="text" name="field2" placeholder="field2"></div>
<div id="RecaptchaField1"></div>
<div><input type="submit"></div>
</form>
<form>
<h1>Form 2</h1>
<div><input type="text" name="field3" placeholder="field3"></div>
<div><input type="text" name="field4" placeholder="field4"></div>
<div id="RecaptchaField2"></div>
<div><input type="submit"></div>
</form>
JavaScript part
<script type="text/javascript">
var CaptchaCallback = function(){
grecaptcha.render('RecaptchaField1', {'sitekey' : '6Lc_your_site_key'});
grecaptcha.render('RecaptchaField2', {'sitekey' : '6Lc_your_site_key'});
};
</script>
recaptcha script url
<script src="//www.google.com/recaptcha/api.js?onload=CaptchaCallback&render=explicit" async defer></script>
Source
http://stackoverflow.com/questions/1241947/how-do-i-show-multiple-recaptchas-on-a-single-page
Simple PHP contact form with Google reCaptcha
So, *everyone* needs a simple form to send emails, via PHP, but we also need a protection agains bots/spammers.
This is code that i’v grabbed from codeforgeek and improved since the original had a few errors that made it impossible to run/work/send email.
It uses Google reCaptcha and you need to register you site/domain on it and grab the site key and the secret key.
The form already has twitter bootstrap classes… but you can remove them! 🙂
The form.php – at the begin
<?php $to = '[email protected]'; $subject = 'Support Message'; // keys from Google reCaptcha https://www.google.com/recaptcha/admin $sitekey = 'recaptcha_site_key'; $secretkey = 'recaptcha_secret_key'; $alert = ''; if ($_SERVER['REQUEST_METHOD'] == 'POST') { $email;$message;$captcha; if(isset($_POST['email'])) $email=$_POST['email']; if(isset($_POST['message'])) $message=$_POST['message']; if(isset($_POST['g-recaptcha-response'])) $captcha=$_POST['g-recaptcha-response']; if(!$captcha) $alert = '<div class="alert alert-warning" role="alert">Please wait until the captcha protection give you a check mark.</div>'; $response=json_decode(file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret='.$secretkey.'&response=".$captcha."&remoteip=".$_SERVER['REMOTE_ADDR'])); if($response->success==false) { /* lets set the error message for the alert... */ if ($alert=='') $alert = '<div class="alert alert-danger" role="alert">Some how you have been detected has a spammer.</div>'; } else { /* the email to you */ $headers = 'From: '.$to.'' . "\r\n" . 'Reply-To: '.$email.'' . "\r\n" . 'X-Mailer: PHP/' . phpversion(); mail($to, $subject, $message, $headers); /* the copy of the email to the *client* */ $headers = 'From: '.$email.'' . "\r\n" . 'Reply-To: '.$to.'' . "\r\n" . 'X-Mailer: PHP/' . phpversion(); mail($email, 'COPY - '. $subject, $message, $headers); /* lets set the success message for the alert... */ $alert = '<div class="alert alert-success" role="alert">Your email has been sent.</div>'; } } ?>
The html form
<?=$alert?> <form id="comment_form" action="" method="post"> <input name="email" type="email" placeholder="Type your email" size="40" class="form-control" value="<?=@$email?>" ><br><br> <textarea name="message" rows="8" cols="39" class="form-control" placeholder="Your message to us..." ><?=@$message?></textarea><br> <div class="g-recaptcha" data-sitekey="<?=$sitekey?>"></div><br> <input type="submit" name="submit" value="Send message" class="btn btn-default"><br> </form>
Some where at the bottom/footer…
<script src='https://www.google.com/recaptcha/api.js'></script>
Hope it helps anyone! 🙂