Google的recaptcha文档并没有像我想象的那样有用,这有点奇怪.我被要求采用当前现有的表格(每天发送几次垃圾邮件)并使用Google的新回程更新.旧的验证码有很多教程,但新的验证码并不多.我基本上只想要一个简单的表单来捕获名称,电子邮件,消息,然后用recaptcha替换我当前的"反机器人字段"(我使用的字段基本上问你2 + 2是什么,如果你输入任何东西,但是4,它不会发送).如果必填字段有效且recaptcha有效,那么我希望它向我发送一封包含表单字段内容的电子邮件.
我经历了简单的步骤:
注册我的网站获取密钥
在我的head标签中添加了这个片段:
<script src='https://www.google.com/recaptcha/api.js'></script>
Run Code Online (Sandbox Code Playgroud)在我的表单末尾添加了此代码段:
<div class="g-recaptcha" data-sitekey="#MYKEY#"></div>
Run Code Online (Sandbox Code Playgroud)在这一点上,recaptcha正好显示出来.但服务器端部分有点令人困惑.
这是我更新的联系表格,其中包含recaptcha:
<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>
Run Code Online (Sandbox Code Playgroud)
这是我当前的POST页面(我不确定在recaptcha代码中添加的位置):
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$human = $_POST['human'];
$from = 'From: My Website';
$to = 'myemail@gmail.com'; …
Run Code Online (Sandbox Code Playgroud)