验证是否勾选了 reCaptcha v2 复选框

Verify reCaptcha v2 checkbox is ticked

我在一天的大部分时间里都在尝试让这个工作正常,但所有似乎都不能完全与我的代码一起工作。我有一个完美运行的 sendmail 脚本,只想添加 google reCaptcha。到目前为止,除非勾选复选框,否则它不会发送联系表,但如果未勾选,我的字段验证器将不起作用,并且不会提醒用户检查 reCaptcha。但是,如果填写了所有字段并勾选了复选框,它就会回显成功消息。

我在 google 上尝试了很多 tuts 并尝试遵循 google 开发人员文档,但似乎无法正确解码我的响应并获得正确的 if、else 语句!

//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         = 'hi@shanemuirhead.co.uk';  // 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;
}

我只是想提醒用户是否未勾选 reCatpcha,如果已勾选则发送,这对我的所有 sendmail.php 正常工作也很好。即如果姓名、电子邮件或消息为空回显一条消息,他们似乎被遗漏了?

$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 ;

试试这个,直接访问这个 $response 可能是行不通的,你试过从调试器记录 $response 是什么吗?

<?php

session_cache_limiter( 'nocache' );
header( 'Expires: ' . gmdate( 'r', 0 ) );
header( 'Content-type: application/json' );
$to         = 'hi@shanemuirhead.co.uk';  // 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($email)){
        $result = array( 'response' => 'error', 'empty'=>'email', 'message'=>'<strong>Error!</strong>&nbsp; Email is empty.' );
        echo json_encode($result );
        die;
    } 
    if(empty($name)){
        $result = array( 'response' => 'error', 'empty'=>'name', 'message'=>'<strong>Error!</strong>&nbsp; Name 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;
    }
    if(empty($_POST['g-recaptcha-response']))  {
        $result = array( 'response' => 'error', 'empty'=>'message', 'message'=>'<strong>Error!</strong>&nbsp; Please complete the Captcha.' );
         echo json_encode($result );
         die;
    }

    if(isset($_POST['g-recaptcha-response']) && !empty($_POST['g-recaptcha-response'])) {
        $secret = '6LeSSI0UAAAAAFnwmu5cj7bk4guiaoXXnlID5-C2';
        $verifyResponse = file_get_contents('https://www.google.com/recaptcha/api/siteverify?secret='.$secret.'&response='.$_POST['g-recaptcha-response']);
        $responseData = json_decode($verifyResponse);
        if($responseData->success == true) {
    $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; Can\'t Send Mail.'  );
    }
    echo json_encode( $result );
    die;

        }
        else
        {
            echo "<p> Sorry verification valid</p>";
        }
   }
?>

因此,通过稍微尝试一下并将我的代码回读给自己,我已经能够让一切正常工作,除了不确定如何重置为验证码!

所以最后我让所有空字段验证器工作,在满足条件后,它会提醒用户勾选验证码(如果尚未勾选),然后它才会发送表单提交并提醒用户成功消息。现在唯一的问题是重置验证码。