【问题标题】:Google Recaptcha Always Return FalseGoogle Recaptcha 总是返回 False
【发布时间】:2016-12-30 03:56:34
【问题描述】:

我正在为我的客户集成一个新的 Google recaptcha,我厌倦了我的工作服务器,一切正常,但在客户端服务器上,它的 go (!$captcha) 部分。
好像Json每次都响应错误,我尝试了不同的方法但失败了。

这是我的代码:

ob_start();
require("class.phpmailer.php");
$captcha = $_POST['g-recaptcha-response'];
$mail = new PHPMailer();

$mail->IsSMTP();                        // set mailer to use SMTP
$mail->Host = "";   // specify main and backup server
$mail->SMTPAuth = true;                 // turn on SMTP authentication
$mail->Username = "";  // SMTP username
$mail->Password = "";               // SMTP password


$Name = $_REQUEST['name'];
$Email = $_REQUEST['email'];
$Subject = $_REQUEST['subject'];
$Remarks = $_REQUEST['remarks'];

$mail->From = "";
$mail->FromName = "Vision Pilates Enquiry Form";

$mail->AddAddress("", "Admin");
//$mail->AddAddress("ellen@example.com");                  // name is optional
$mail->AddReplyTo("$Email", "$Name");

$mail->WordWrap = 50;                                 // set word wrap to 50 characters
//$mail->AddAttachment("/var/tmp/file.tar.gz");         // add attachments
//$mail->AddAttachment("/tmp/image.jpg", "new.jpg");    // optional name
//$mail->IsHTML(true);                                  // set email format to HTML

$mail->Subject = "Enquiry from website: $Subject";
$mail->Body    = "<strong>Name:</strong> $Name <br><strong>Email:</strong> $Email <br><strong>Subject:</strong> $Subject <br><strong>Remarks:</strong> $Remarks <br>"; 
$mail->AltBody = "This is the body in plain text for non-HTML mail clients";
if($_SERVER["REQUEST_METHOD"] === "POST") {
    //form submitted

    //check if other form details are correct

    //verify captcha
    if (!$captcha) {
        echo '<h2>Please check the the captcha form.</h2>';
        exit;
    }
    $response = file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret==SecretKey=&response=" . $captcha . "&remoteip=" . $_SERVER['REMOTE_ADDR']);
    if ($response . success == false) {
        echo '<h2>You are spammer ! Get the @$%K out</h2>';
    } else {
        if (!$mail->Send()) {
            header("location: error.htm");
            exit;
        } else {
            header("location: sent.htm");
        }
    }
}

提交后的最终结果是

请检查验证码表格。

谢谢

【问题讨论】:

  • 任何一个?谁能帮忙?
  • 你是通过get而不是post发送数据,你必须使用curl

标签: php phpmailer recaptcha


【解决方案1】:

方法必须根据postrecaptcha documentation

API 请求

网址:https://www.google.com/recaptcha/api/siteverify

方法:发布

function VerifyRecaptcha($g_recaptcha_response) {
    $ch = curl_init();
    $curlConfig = array(
        CURLOPT_URL            => "https://www.google.com/recaptcha/api/siteverify",
        CURLOPT_POST           => true,
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_POSTFIELDS     => array(
            'secret' => 'your secret goes here <<<<<<<',
            'response' => $g_recaptcha_response,
            'remoteip' => $_SERVER['REMOTE_ADDR']
        )
    );
    curl_setopt_array($ch, $curlConfig);
    if($result = curl_exec($ch)){
        curl_close($ch);
        $response = json_decode($result);
        return $response->success;
    }else{
        var_dump(curl_error($ch)); // this for debug remove after you test it
        return false;
    }
}

那就这样用吧

if(!VerifyRecaptcha($_POST['g-recaptcha-response']))
{
    echo '<h2>Please check the the captcha form.</h2>';
    exit;
}

【讨论】:

    【解决方案2】:

    你做事的顺序很有趣。在设置任何其他内容之前检查验证码 - 如果验证码错误,您将不需要 PHPMailer 实例。

    我也怀疑你的网址坏了:

    $response = file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret==SecretKey=&response=" . $captcha . "&remoteip=" . $_SERVER['REMOTE_ADDR']);
    

    你有一个额外的= 在那里,你没有逃避你的参数。这可能会导致 URL 无效——recaptcha 通常要求输入两个单词,并且 URL 中的空格无效,所以我建议您这样做:

    $response = file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=SecretKey=&response=" . rawurlencode($captcha) . "&remoteip=" . rawurlencode($_SERVER['REMOTE_ADDR']));
    

    您还使用旧版本的 PHPMailer;请get the latest

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-08-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-07-20
      • 2021-11-05
      相关资源
      最近更新 更多