Search code examples
phprecaptchaserver-side

Verify reCaptcha v2 checkbox is ticked


I have tried for the majority of the day to get this working but all seem to not fully work with my code. I have a perfectly working sendmail script and just want to add in the google reCaptcha. So far it won't send contact form unless checkbox is ticked, but if it's not ticked my field validators don't work and there is not alert to tell the user to check the reCaptcha. But if all fields are filled out and checkbox is ticked it does echo the success message.

I have tried so many tuts on google and tried to follow the google developer docs but can't seem to decode my response correctly and get the if, else statements right!

//Checking For reCAPTCHA
$captcha;
if (isset($_POST['g-recaptcha-response'])) {
$captcha = $_POST['g-recaptcha-response'];
}
// Checking For correct reCAPTCHA
$response = 
file_get_contents("https://www.google.com/recaptcha/api/siteverify? 
secret=SECRETKEY&response=" . $captcha . "&remoteip=" . 
$_SERVER['REMOTE_ADDR']);
if (!$captcha || $response.success == false) {
echo "Your CAPTCHA response was wrong.";
exit ;
} else {

session_cache_limiter( 'nocache' );
header( 'Expires: ' . gmdate( 'r', 0 ) );
header( 'Content-type: application/json' );
$to         = '[email protected]';  // put your email here
$email_template = 'simple.html';
$subject    = strip_tags($_POST['vsubject']);
$email       = strip_tags($_POST['vemail']);
$name       = strip_tags($_POST['vname']);
$message    = nl2br( htmlspecialchars($_POST['vmessage'], ENT_QUOTES) );
$result     = array();

if(empty($name)){
    $result = array( 'response' => 'error', 'empty'=>'name', 'message'=>'<strong>Error!</strong>&nbsp; Name is empty.' );
    echo json_encode($result );
    die;
} 
if(empty($email)){
    $result = array( 'response' => 'error', 'empty'=>'email', 'message'=>'<strong>Error!</strong>&nbsp; Email is empty.' );
    echo json_encode($result );
    die;
} 
if(empty($message)){
     $result = array( 'response' => 'error', 'empty'=>'message', 'message'=>'<strong>Error!</strong>&nbsp; Message body is empty.' );
     echo json_encode($result );
     die;
}



$headers  = "From: " . $name . ' <' . $email . '>' . "\r\n";
$headers .= "Reply-To: ". $email . "\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=UTF-8\r\n";
$templateTags =  array(
    '{{subject}}' => $subject,
    '{{email}}'=>$email,
    '{{message}}'=>$message,
    '{{name}}'=>$name
    );
$templateContents = file_get_contents( dirname(__FILE__) . '/inc/'.$email_template);
$contents =  strtr($templateContents, $templateTags);
if ( mail( $to, $subject, $contents, $headers ) ) {
    $result = array( 'response' => 'success', 'message'=>'<strong>Thank You!</strong>&nbsp; Your email has been delivered.' );
} else {
    $result = array( 'response' => 'error', 'message'=>'<strong>Error!</strong>&nbsp; Cann\'t Send Mail.'  );
}
echo json_encode( $result );
die;
}

I just want to alert user is reCatpcha is not ticked and send if it is, also would be nice for all my sendmail.php to work correctly. i.e if name, email, or message is empty echo a message, they seem to be getting missed?


Solution

  • $response = 
    file_get_contents("https://www.google.com/recaptcha/api/siteverify? 
    secret=SECRETKEY&response=" . $captcha . "&remoteip=" . 
    $_SERVER['REMOTE_ADDR']);
    $s = json_decode($response);
    if (!$captcha || $s.success == false) {
    echo "Your CAPTCHA response was wrong.";
    exit ;
    

    Try this, it's possible that accessing this $response directly isn't working, have you tried logging what $response is from the debugger?