//ads:
?>
Adding reCAPTCHA v2 into my PHP file
PHP Snippet 1:
<?php
if(isset($_POST['full_name']) && isset($_POST['full_name']) ){
$full_name = $_POST['full_name'];
$email = $_POST['email'];
$from_name_email = $full_name . '<'.$email.'>';
}
if(isset($_POST['subject'])){
$subject = $_POST['subject'];
}
if(isset($_POST['message'])){
$message = $_POST['message'];
}
}if(isset($_POST['g-recaptcha-response'])){
$captcha=$_POST['g-recaptcha-response'];
$to = "[email protected]";
$subject = "Contact Form";
$headers .= 'From: ' . $from_name_email . "\r\n";
}
if(!$captcha){
echo 'Please check the the captcha form.';
exit;
}
$response=file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=YOUR SECRET KEY GOES HERE&response=".$captcha."&remoteip=".$_SERVER['REMOTE_ADDR']);
if($response.success==false)
{
echo 'You are spammer!';
}else
{
mail ($to, $subject, $message, $headers);
echo "Your message has been sent! Thank you.";
}
?>
PHP Snippet 2:
<?php
if(isset($_POST['full_name']) && isset($_POST['full_name']) ){
$full_name = $_POST['full_name'];
$email = $_POST['email'];
$from_name_email = $full_name . '<'.$email.'>';
}
if(isset($_POST['subject'])){
$subject = $_POST['subject'];
}
if(isset($_POST['message'])){
$message = $_POST['message'];
}
}if(isset($_POST['g-recaptcha-response'])){
$captcha=$_POST['g-recaptcha-response'];
$to = "[email protected]";
$subject = "Contact Form";
$headers .= 'From: ' . $from_name_email . "\r\n";
}
if(!$captcha){
echo 'Please check the the captcha form.';
exit;
}
//change is here
$response=file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=USE PRIVATE KEY HERE&response=".$_POST['g-recaptcha-response'."&remoteip=".$_SERVER['REMOTE_ADDR']);
//change is here
$response = json_decode($response);
//change is here
if($response->success==false)
{
echo 'You are spammer!';
}else
{
mail ($to, $subject, $message, $headers);
echo "Your message has been sent! Thank you.";
}
?>
PHP Snippet 3:
if(!isset($_POST['g-recaptcha-response']) || empty($_POST['g-recaptcha-response'])) {
echo 'reCAPTHCA verification failed, please try again.';
} else {
$secret = 'google_secret_key';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://www.google.com/recaptcha/api/siteverify?secret='.$secret.'&response='.$_POST['g-recaptcha-response']);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
$response = json_decode($response);
if($response->success) {
// What happens when the CAPTCHA was entered incorrectly
echo 'Successful .';
} else {
// Your code here to handle a successful verification
echo 'reCAPTHCA verification failed, please try again.';
}
}