【问题标题】:reCAPTCHA v2. Need detailed instructions on how to include the verification验证码 v2。需要有关如何包含验证的详细说明
【发布时间】:2020-07-19 20:05:23
【问题描述】:

我已经为此苦苦挣扎了一段时间,需要一些帮助。

我已经设法在我的在线表单中添加了一个 reCAPTCHA 复选框,但在验证时我不知道该怎么做。

我使用了Checkbox instructions 中所述的“自动呈现 reCAPTCHA 小部件”选项。

但我卡在verification instructions 上。

我的表单代码很简单:

<form name="myForm" action="mail.php" onsubmit="return validateForm()" method="POST">
<div>
  <label for="name">Name:</label><br /><input id="name" type="text" name="name" class="formthin" required />
</div>

<div>
  <label for="email">Email:</label><br />
  <input id="email" type="email" name="email" class="formthin" required />
</div>

<div>
  <label for="message">Comments, Questions, Whatever:</label><br />
  <textarea id="message" name="message" rows="4" cols="4" class="formbig" required></textarea>
</div>

<div>
  <div class="g-recaptcha" data-sitekey="site-key"></div>
</div>

<div>
  <input type="submit" value="Submit" class="formsubmit" />
</div>

</form>

我在“&lt;head&gt;&lt;/head&gt;”标签之间添加了以下内容。

<script src="https://www.google.com/recaptcha/api.js" async defer></script>

到目前为止,页面上的一切看起来都不错。

为了更好的衡量,这里是“mail.php”文件中的代码:

<?php
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$formcontent=" From: \n $name \n\n Message: \n $message";
$recipient = "my email address";
$subject = "Website Contact Form";
$mailheader = "From: $email \r\n";
$forward = 1;
$location = "url of thank you page";
mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
if ($forward == 1) { 
    header ("Location:$location"); 
} 
else { 
    echo "Thank you message."; 
} 
?>

这是我需要帮助的地方。 我不确定我应该使用三个验证选项中的哪一个,如何实现选择,或者 API 响应的代码应该去哪里。

例如,如果我使用第一个选项,“g-recaptcha-reponse”POST 参数是否进入我的初始&lt;form&gt; 标记? API 请求上的注释似乎表明我需要第二个 POST 方法。我不确定如何将它与我当前的结合或实施。

API 响应代码是否在我的“mail.php”文件中?或者在我的表单页面上的“&lt;script&gt;&lt;/script&gt;”标签之间? (在头部还是在表格下方?)

我的“秘钥”在哪里发挥作用?我没有看到任何关于如何包含它的说明。

我浏览了论坛,发现了一个似乎相关的先前问题: “Google reCaptcha v2 (Checkbox) Verification” 原始发布的代码看起来与我的非常相似。 (在 html 页面和 php 操作脚本上)

回复是第二个 php 脚本,看起来它包含密钥,但没有说明该 php 代码应该去哪里。我是否将其与表格一起放入文件中?它是添加还是替换 mail.php 操作脚本?如果它替换了脚本,如何处理来自实际表单的响应?

任何帮助将不胜感激。 谢谢。

++++++++++++++++++++++++++++

我已按照@Bazaim 发送的链接中的说明进行操作。 这些是我采取的步骤:

  1. 下载“recaptcha-master”文件夹并将其添加到我的网站目录。

  2. 在我的终端中运行“composer”安装脚本。

  3. 将“composer.phar”文件移动到“recaptcha-master”文件夹中。

  4. 在“composer.json”文件中添加了以下内容:

    “要求”:{ “谷歌/recaptcha”:“^1.2” }

  5. 将“require_once ...”脚本添加到我的“mail.php”文件的底部,如下所示:

    setExpectedHostname('我的域名.com') ->验证($gRecaptchaResponse,$remoteIp); if ($resp->isSuccess()) { // 已验证! } 别的 { $errors = $resp->getErrorCodes(); } ?>

到目前为止一切顺利。当我转到我的表单并单击“我不是机器人”的“复选框”时,系统会提示我进行图像识别并在“我不是机器人”旁边获得绿色的“复选框”。

但是,当我提交表单时,我的“mail.php”不再起作用。它尝试在浏览器窗口中加载,而不是向我发送电子邮件。 此外,reCAPTCHA 复选框不是“必需”才能点击提交。

【问题讨论】:

  • 你读过这个github.com/google/recaptcha吗?
  • 嗨@Bazaim - 感谢您的链接。我已经取得了一些进展。我的回复太长,无法发表评论,因此我将编辑我的原始帖子。
  • 我的代码在我的编辑中没有正确显示。这是我的新 mail.php 文件的样子:
  • &lt;?php $name = $_POST['name']; $email = $_POST['email']; $message = $_POST['message']; $formcontent=" From: \n $name \n\n Message: \n $message"; $recipient = "email address"; $subject = "Website Contact Form"; $mailheader = "From: $email \r\n"; $forward = 1; $location = "url of thank you page";
  • mail($recipient, $subject, $formcontent, $mailheader) or die("Error!"); if ($forward == 1) { header ("Location:$location"); } else { echo "Thank you message."; }

标签: recaptcha


【解决方案1】:

我让它工作了。这是我最终做的:

从我的 mail.php 文件中删除了额外的 PHP 脚本。 (前几篇文章中提到的“require_once”内容)我的 PHP 脚本在我添加之前正在运行,所以我想将它恢复到工作版本。

我尝试将 reCAPTCHA 设为一项要求,因此我再次搜索并找到 instructions on adding javascript and css 并将其添加到我页面上的代码中。 (这是我在原帖中试图解决的问题之一)

JavaScript:

window.onload = function() {
    var $recaptcha = document.querySelector('#g-recaptcha-response');

    if($recaptcha) {
        $recaptcha.setAttribute("required", "required");
    }
};

CSS:

#g-recaptcha-response {
    display: block !important;
    position: absolute;
    margin: -78px 0 0 0 !important;
    width: 302px !important;
    height: 76px !important;
    z-index: -999999;
    opacity: 0;
}

添加到我的联系页面上的&lt;head&gt;&lt;/head&gt; 代码。

Bingo - 一切正常。

我不确定验证信息是否真正被使用。 (我在原始帖子中试图弄清楚的另一件事)现在我不确定它是否真的有必要。表单中需要 reCAPTCHA,表单正在提交中。

无论如何,感谢@Bazaim 的帮助并让我走上正轨。 希望这可以帮助其他可能遇到此问题的人。

【讨论】:

    猜你喜欢
    • 2011-09-24
    • 1970-01-01
    • 1970-01-01
    • 2010-09-11
    • 2011-05-04
    • 2016-04-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多