Google 重新验证 (PHP)

Google Recaptcha (PHP)

所以,这看起来很简单,我已经在我的 website 中添加了一个 Google 验证码,下面是 HTML 代码。

<script src='https://www.google.com/recaptcha/api.js'></script>
<div class="g-recaptcha" data-sitekey="My key would be here"></div>

但是,人们仍然可以在不完成验证码的情况下填写表格和发送邮件。 (所以他们不必解决任何他们可以直接解决的难题,这当然让我对机器人很脆弱)

所以,我基本上需要 PHP 代码来检查用户是否确实 "Ticked" 或 "Completed" Recaptcha。这样他们就可以继续发送邮件了。

我的PHP代码:

if ($_POST['submit']) {
        if ($email != '') {
            if ($human == '4') {                 
                if (mail ($to, $subject, $body, $from)) { 
                    echo '<p>You have successfully submitted your information to PS4RS. Subscribers to our mailing list will begin to periodically receive updates.</p>';
                } else { 
                    echo '<p>Something went wrong, go back and try again!</p><p><input type="button" value="Go Back" onclick="history.back(-1)" class="goback" /></p>'; 
                } 
            } else if ($_POST['submit'] && $human != '4') {
                echo '<p>You answered the anti-spam question incorrectly!</p><p><input type="button" value="Go Back" onclick="history.back(-1)" class="goback" /></p>';
            }
        } else {
            echo '<p>You need to fill in all required fields!!</p><p><input type="button" value="Go Back" onclick="history.back(-1)" class="goback" /></p>';
        }
    }
?>

我真的不知道如何在 PHP 中编码,这是我最好的尝试。

这来自官方Google开发网页:

<?php
    require_once('recaptchalib.php');
    $privatekey = "your_private_key";
    $resp = recaptcha_check_answer ($privatekey,
                            $_SERVER["REMOTE_ADDR"],
                            $_POST["recaptcha_challenge_field"],
                            $_POST["recaptcha_response_field"]);

    if (!$resp->is_valid) {
        // What happens when the CAPTCHA was entered incorrectly
        die ("The reCAPTCHA wasn't entered correctly. Go back and try it again." .
     "(reCAPTCHA said: " . $resp->error . ")");
    } else {
        // Your code here to handle a successful verification
    }
?>

告诉我这是否有帮助

这不是我原来的答案,是我找到的here

<?php
  $response=file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=". $yoursecret."&response=".$_POST['g-recaptcha-response']."&remoteip=".$_SERVER['REMOTE_ADDR']);
  $googleobj = json_decode($response);
  $verified = $googleobj->success;
  if ($verified === true){
    //do stuff
  }

所以为了你的目的...

<?php
if($_POST['submit']) {
  $response = file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=". $yoursecret."&response=".$_POST['g-recaptcha-response']."&remoteip=".$_SERVER['REMOTE_ADDR']);
  $googleobj = json_decode($response);
  $verified = $googleobj->success;
  if($verified === true) {
    if(mail($to, $subject, $body, $from)) { 
      echo '<p>You have successfully submitted your information to PS4RS. Subscribers to our mailing list will begin to periodically receive updates.</p>';
    } else { 
      echo '<p>Something went wrong, go back and try again!</p><p><input type="button" value="Go Back" onclick="history.back(-1)" class="goback" /></p>'; 
    } 
  }
}
?>


请务必将您的 SECRET KEY 添加到 $yoursecret

(与站点密钥不同)


希望有帮助