如何验证已检查reCaptcha

时间:2019-05-23 16:33:51

标签: php recaptcha

我正在创建一个联系表,如果选中了Google reCaptcha v2,则应发送邮件。 现在,即使选中了reCaptcha,它也会回显我的文字。

我尝试在reCaptcha片段中四处移动,并尝试执行if(isset,但似乎不起作用。

  $email = $_POST['email'];
  $subject = $_POST['subject'];
  $message = $_POST['message'];

  $errorEmpty = false;
  $errorEmail = false;
  $errorCaptcha = false;

  if(empty($name) || empty($email) || empty($subject) || empty($message)){
    echo "<span style='color: red;font-weight: bold;'>Fyll i alla fält!</span>";
    $errorEmpty = true;
  }
  if(empty($_POST['g-recaptcha-response'])){
    echo "<span style='color: red;font-weight:bold;'>reCaptchan är inte ifylld!</span>";
    $errorCaptcha = true;
  }
   elseif(!filter_var($email, FILTER_VALIDATE_EMAIL)){
     echo "<span style='color: red;font-weight: bold;'>Du måste skriva in en giltig e-mail adress!</span>";
     $errorEmail = true;
   }
    else{
      echo "<span style='color: green;font-weight: bold;'>Ditt meddelande skickades, vi återkommer inom max 3 arbetsdagar!</span>";
    }

    if(isset($_POST['g-recaptcha-response']) && !empty($_POST['g-recaptcha-response'])){
      $secret = '';
      $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){
  $to = $email;
  $subject = $subject;
  $text = "Name: $name\n From: $name\n Message: $message";
  $headers = "From: $email";
  if($errorEmpty == false && $errorEmail == false && $errorCaptcha == false){
  mail($to,$subject,$text,$headers);
  }
 }
} // ReCaptcha 2

如果要检查reCaptcha,我希望激活mail()函数,否则我想向用户回显文本。

2 个答案:

答案 0 :(得分:1)

很难说出您在哪里遇到问题。您当前的代码将始终echo发出某种消息(尽管您可能需要显示一条消息,成功还是失败)。

如果您说无法获得if($responseData->success){来正确验证复选框,我建议您通过使用var_dump($responseData)查看响应数据,看看是否< em> Google 试图告诉您一些信息(错误的秘密密钥,域名等)。

作为一种替代方法,您可以考虑使用 Google的 PHP reCaptcha库作为一种更轻松的方法来处理这种情况-例如,我的代码:

function validateRecaptcha(?String $gRecaptchaResponse) {

    /* API source from: https://github.com/google/recaptcha */

    $recaptcha = new ReCaptcha\ReCaptcha(***SECRET HERE***);

    $resp = $recaptcha->verify($gRecaptchaResponse, $_SERVER['REMOTE_ADDR']);

    return $resp->isSuccess();

}

PS:请谨慎使用empty来验证用户输入。如果用户决定仅以0作为内容提交邮件,则您的if(...|| empty($message)){检查将失败。

请参阅:https://www.php.net/manual/en/function.empty.php

答案 1 :(得分:0)

我查看了您的代码,发现了一些错误。我在下面粘贴了更正的代码,请尝试一下,让我知道它是否可以解决您的问题。

<?php
$error = false;
$output = '';

$name    = $_POST["name"];
$email   = $_POST['email'];
$subject = $_POST['subject'];
$message = $_POST['message'];

if (empty($name) || empty($email) || empty($subject) || empty($message)) {
  $output = "<span style='color: red;font-weight: bold;'>Fyll i alla fält!</span>";
  $error = true;
}
if (empty($_POST['g-recaptcha-response'])) {
  $output = "<span style='color: red;font-weight:bold;'>reCaptchan är inte ifylld!</span>";
  $error = true;
}
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
  $output =  "<span style='color: red;font-weight: bold;'>Du måste skriva in en giltig e-mail adress!</span>";
  $error = true;
}
if ($error) {
  echo $output;
} else {
  $secretKey = "YOUR-SECRET-KEY";
  $captcha=$_POST['g-recaptcha-response'];
  $url = 'https://www.google.com/recaptcha/api/siteverify?secret=' .
    urlencode($secretKey) .  '&response=' . urlencode($captcha);
  $response = file_get_contents($url);
  $responseKeys = json_decode($response, true);
  if ($responseKeys["success"]) {

    // mail then
    $to = $email;
    $email_subject = $subject;
    $email_body = "Name: $name\n From: $name\n Message: $message";
    //Send the email!
    $mail_check = mail($to, $email_subject, $email_body);
    if ($mail_check) {
      echo "Mail Sent!";
    } else {
      echo 'Mail Failed';
    }
  } else {
    echo 'Response not Success';
  }
}
?>