簡體   English   中英

PHP表單+ Google reCAPTCHA

[英]PHP form + Google reCAPTCHA

Google的recaptcha文檔並沒有像我想象的那樣有用,這有點奇怪。 我被要求采用當前現有的表格(每天發送幾次垃圾郵件)並使用Google的新回程更新。 舊的驗證碼有很多教程,但新的驗證碼並不多。 我基本上只想要一個簡單的表單來捕獲名稱,電子郵件,消息,然后用recaptcha替換我當前的“反機器人字段”(我使用的字段基本上問你2 + 2是什么,如果你輸入任何東西,但是4,它不會發送)。 如果必填字段有效且recaptcha有效,那么我希望它向我發送一封包含表單字段內容的電子郵件。

我經歷了簡單的步驟:

  1. 注冊我的網站獲取密鑰

  2. 在我的head標簽中添加了這個片段:

     <script src='https://www.google.com/recaptcha/api.js'></script> 
  3. 在我的表單末尾添加了此代碼段:

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

歡迎任何幫助。 我覺得這可能是一個非常普通的人,人們試圖將它實現到他們當前的工作形式。

請訪問以下鏈接: 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() 我建議你在程序開頭檢查這個。

更新

JSON對象的解碼如下所示:

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

更新

有時, 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>';
        }
    }
?>

也許上面的答案有點過時,因為谷歌現在正在使用reCaptcha nocaptcha。 我在這里找到了一個更簡單,更完整的答案,可用於單獨的php電子郵件文件。

該解決方案有一個簡單的電子郵件表單,其中包含姓名和電子郵件以及單獨的php文件以提交表單 你應該可以從那里繼續並相應地調整你的表格。 解決方案對我有用。

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

並鏈接到教程:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM