【问题标题】:Google Recaptcha (PHP)谷歌验证码 (PHP)
【发布时间】:2016-07-07 05:47:21
【问题描述】:

所以,看起来很简单,我在我的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 代码来检查用户是否真的“勾选”或“完成”了 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 编码,这是我最好的尝试。

【问题讨论】:

标签: php html recaptcha


【解决方案1】:

这不是我原来的答案,我找到了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

(这与站点密钥不同)


希望有帮助

【讨论】:

    【解决方案2】:

    这是来自 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
        }
    ?>
    

    告诉我这是否有帮助

    【讨论】:

    • recaptcha php api 已贬值
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多