PHP表单+;谷歌雷帕查
很奇怪,谷歌的recaptcha文档并没有我想象的那么有用。我被要求使用一个现有的表单(每天都会收到几次垃圾邮件),并用谷歌的新recaptcha更新它。有很多关于旧的验证码的教程,但是对于新的验证码没有那么多。我基本上只需要一个简单的表单来捕获姓名、电子邮件、消息,然后用recaptcha替换我当前的“反机器人字段”(我使用了一个字段,基本上询问您2+2是什么,如果您输入了任何内容,但输入了4,则不会发送)。如果必填字段有效,并且recaptcha有效,那么我希望它向我发送一封包含表单字段内容的电子邮件 我完成了简单的步骤:PHP表单+;谷歌雷帕查,php,forms,recaptcha,contact,Php,Forms,Recaptcha,Contact,很奇怪,谷歌的recaptcha文档并没有我想象的那么有用。我被要求使用一个现有的表单(每天都会收到几次垃圾邮件),并用谷歌的新recaptcha更新它。有很多关于旧的验证码的教程,但是对于新的验证码没有那么多。我基本上只需要一个简单的表单来捕获姓名、电子邮件、消息,然后用recaptcha替换我当前的“反机器人字段”(我使用了一个字段,基本上询问您2+2是什么,如果您输入了任何内容,但输入了4,则不会发送)。如果必填字段有效,并且recaptcha有效,那么我希望它向我发送一封包含表单字段内容
<script src='https://www.google.com/recaptcha/api.js'></script>
<div class="g-recaptcha" data-sitekey="#MYKEY#"></div>
<form method="post" action="contact-post.php">
<label>Your Name (required):</label>
<input name="name" type="text" placeholder="Enter your name here">
<label>Email Address (required):</label>
<input name="email" type="email" placeholder="Enter your email address here">
<label>Your Message (required):</label>
<textarea name="message" placeholder="Write your message here"></textarea>
<div style="margin-top:20px;" class="g-recaptcha" data-sitekey="#MYKEY#"></div>
<input id="submit" name="submit" type="submit" value="Submit Form">
</form>
您的姓名(必填):
电子邮件地址(必填):
您的信息(必填):
这是我当前的帖子页面(我不确定在哪里添加recaptcha代码):
欢迎任何帮助。我觉得这可能是一个非常普遍的现象,人们试图将其应用到当前的工作形式中。查看此链接:
简单地说,你应该向
https://www.google.com/recaptcha/api/siteverify?secret=YOUR_SECRET&response=RESPONSE_CAME_FROM_YOUR_FORM&remoteip=USER_IP_ADDRESS
如果您的密钥是您在ReCAPTCHA站点上收到的密钥,则可以通过$\u服务器
数组接收用户的IP地址,并且您的表单是随表单发送的字符串。它存储在$\u POST['g-recaptcha-response']
中
你可以通过file\u get\u contents($url)
like
$data = file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=YOUR_SECRET&response=RESPONSE_CAME_FROM_YOUR_FORM&remoteip=USER_IP_ADDRESS");
在$data
中,您将收到JSON对象,其中包含您正在查找的success
字段。如果成功是假的,那么它不是人,您应该exit()
。我建议你在课程开始时检查一下
更新:
JSON对象的解码如下所示:
$data = json_decode($data); // This will decode JSON to object
if(!$data->success)
exit();
更新:
有时,file\u get\u contents($url)
将无法设置安全的https连接。相反,您可以使用open\uhttps\uurl($url)
使您的代码看起来像:
<?php
$your_secret = "<secret_key_you_received_from_recaptcha_site>";
$client_captcha_response = $_POST['g-recaptcha-response'];
$user_ip = $_SERVER['REMOTE_ADDR'];
$captcha_verify = open_https_url("https://www.google.com/recaptcha/api/siteverify?secret=$your_secret&response=$client_captcha_response&remoteip=$user_ip");
$captcha_verify_decoded = json_decode($captcha_verify);
if(!$captcha_verify_decoded->success)
die('DIRTY ROBOT');
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$human = $_POST['human'];
$from = 'From: My Website';
$to = 'myemail@gmail.com';
$subject = 'Request Form';
$body = "Name: $name \n E-Mail: $email \nMessage:\n$message";
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>';
}
}
?>
也许上述答案有点过时,因为谷歌现在使用的是reCaptcha nocaptcha。我在这里找到了一个更简单、更完整的答案,可用于单独的php电子邮件文件 该解决方案有一个带有名称和电子邮件的简单电子邮件表单,以及一个单独的php文件来提交表单。你应该能够从那里继续,并相应地调整你的形式。这个解决方案对我有效 并链接到教程:
FWIW,我放弃了谷歌重新验证码,转而使用内置在我框架中的自定义验证码解决方案。不管出于什么原因,我无法让这个小部件在所有浏览器中都可靠地显示出来。是的,我肯定看过那个网站,似乎我需要更多的编码知识来了解它的概念。你解释得很好。但是,弄清楚如何修改我的帖子页面以及在哪里放入代码是我遇到麻烦的棘手部分。我添加了一些修复程序和示例代码。希望你现在一切都好=)嗨,亚历克斯,因为这是一个只有链接的答案,可能会被删除。如果您发现确实存在重复项,请将其标记为重复项。如果你想提供一个独立的答案,请保留链接作为参考。我不知道它是否重复。有时这个问题定义得很糟糕,可能有很多答案。无论如何,我强烈支持消除重复,但不确定标记系统的效率。为查询者指出正确的方向也会很有帮助。我也不能(缺少领域知识),添加链接是可以的,但是只有链接的答案通常会被删除,在这种情况下,请添加您提供的第一个链接的摘要。我对PHP是全新的(尝试从.Net扩展技能集)我花了好几个小时试着让它发挥作用。我在做curl调用,但无论我用了多少种不同的方法,都无法正确读取响应。你上面的链接(codefforgek.com)救了我一命,使我不至于打我相信的东西。谢谢:-)
<?php
$your_secret = "<secret_key_you_received_from_recaptcha_site>";
$client_captcha_response = $_POST['g-recaptcha-response'];
$user_ip = $_SERVER['REMOTE_ADDR'];
$captcha_verify = open_https_url("https://www.google.com/recaptcha/api/siteverify?secret=$your_secret&response=$client_captcha_response&remoteip=$user_ip");
$captcha_verify_decoded = json_decode($captcha_verify);
if(!$captcha_verify_decoded->success)
die('DIRTY ROBOT');
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$human = $_POST['human'];
$from = 'From: My Website';
$to = 'myemail@gmail.com';
$subject = 'Request Form';
$body = "Name: $name \n E-Mail: $email \nMessage:\n$message";
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>';
}
}
?>