首页 文章

PHP形成Google reCAPTCHA

提问于
浏览
8

Google的recaptcha文档并没有像我想象的那样有用,这有点奇怪 . 我被要求采用当前现有的表格(每天发送几次垃圾邮件)并使用Google的新回程更新 . 旧的验证码有很多教程,但新的验证码并不多 . 我基本上只想要一个简单的表单来捕获名称,电子邮件,消息,然后用recaptcha替换我当前的“反机器人”字段(我使用的字段基本上问你2 2是什么,如果你输入任何东西,但4 ,它不会发送) . 如果必填字段有效且recaptcha有效,那么我希望它向我发送一封包含表单字段内容的电子邮件 .

我经历了简单的步骤:

  • 注册了我的网站以获取密钥

  • 在我的头标记中添加了此代码段:

<script src='https://www.google.com/recaptcha/api.js'></script>
  • 在我的表单末尾添加了此代码段:
<div class="g-recaptcha" data-sitekey="#MYKEY#"></div>

在这一点上,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>

这是我当前的POST页面(我不确定在recaptcha代码中添加的位置):

<?php
        $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>';
            }
        }
    ?>

欢迎任何帮助 . 我觉得这可能是一个非常普通的人,人们试图将它实现到他们当前的工作形式 .

2 回答

  • 1

    看看这个链接:https://developers.google.com/recaptcha/docs/verify

    简而言之,你应该提出要求

    https://www.google.com/recaptcha/api/siteverify?secret=YOUR_SECRET&response=RESPONSE_CAME_FROM_YOUR_FORM&remoteip=USER_IP_ADDRESS
    

    如果YOUR_SECRET是您在ReCAPTCHA网站上收到的密钥,则可以通过 $_SERVER 数组接收USER_IP_ADDRESS,并且RESPONSE_CAME_FROM_YOUR_FORM是与您的表单一起发送的字符串 . 它存储在 $_POST['g-recaptcha-response'] 中 .

    你可以通过 file_get_contents($url) 这样做

    $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 中,您将收到包含 success 字段的JSON对象,您正在寻找该字段 . 如果成功是错误的,那么它不是人类,你应该 exit() . 我建议你在程序开头检查这个 .

    Update

    JSON对象的解码如下所示:

    $data = json_decode($data); // This will decode JSON to object
    if(!$data->success) 
        exit();
    

    Update

    有时, file_get_contents($url) 将无法设置安全的https连接 . 相反,您可以使用 open_https_url($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>';
            }
        }
    ?>
    
  • 6

    也许上面的答案有点过时,因为谷歌现在正在使用reCaptcha nocaptcha . 我在这里找到了一个更简单,更完整的答案,可用于单独的php电子邮件文件 .

    该解决方案有一个简单的电子邮件表单,其中包含姓名和电子邮件以及单独的php文件以提交表单你应该可以从那里继续并相应地调整你的表格 . 解决方案对我有用 .

    https://stackoverflow.com/a/27439796/3934886

    并链接到教程:

    http://codeforgeek.com/2014/12/google-recaptcha-tutorial/

相关问题