2017-03-16 97 views
2

所以,我想通過谷歌來實現一些網站全新看不見驗證碼。缺少輸入響應|隱形驗證碼

我完全按照這些步驟操作,但它不斷給我提供缺失 - 輸入 - 響應錯誤。

HTML代碼:

<form id="subscribe-form" class="form-inline" action="phpScripts/subscribe/subscribeHandler.php" method="post"> 
    <div class="input-group"> 
     <input type="email" name="email" class="form-control" size="50" placeholder="Email Address" required> 
     <div class="input-group-btn"> 
      <button class="g-recaptcha btn btn-danger" data-sitekey="6LfoNhkUAAAAAEcQFx8vGHZKHXsZ_q0j2KDnmU9M" data-callback="submitForm">Subscribe</button> 
     </div> 
    </div> 
</form> 

PHP代碼:

<?php 
include 'databaseConnection.php'; 
if($_POST){ 
      $secret = "MY SECRET KEY"; 
      $captcha= $_POST['g-recaptcha-response']; 
      $ip = $_SERVER['REMOTE_ADDR']; 
      $url= file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=$secret&response=$captcha&remoteip=$ip"); 
      print_r($url); 
      $decodedResponse = json_decode($url, TRUE); 

      if($decodedResponse['success'] == 1){//code here} 

所以,我在想,我的$驗證碼變量可以從G-驗證碼響應的POST沒有 「捕獲」 沒什麼。但爲什麼呢,這正是Google所說的,正如舊版reCaptcha v2所說的那樣。

藏漢,我已經包括<script src='https://www.google.com/recaptcha/api.js'></script>

回答

1

的問題可能是,你可能追平了功能按鈕。

儘量實現他們給你創建你的鑰匙時,代碼:

<form id="subscribe-form" class="form-inline" action="phpScripts/subscribe/subscribeHandler.php" method="post"> 
    <div class="input-group"> 
     <input type="email" name="email" class="form-control" size="50" placeholder="Email Address" required> 
     <div class="g-recaptcha" data-sitekey="{keyhere}"></div> 
     <div class="input-group-btn"> 
      <button class="btn btn-danger" data-sitekey=" data-callback="submitForm">Subscribe</button> 
     </div> 
    </div> 
</form> 

對於PHP邏輯驗證:

if ($_POST['g-recaptcha-response']) { 
$secret = '{keyhere}'; 
$response = file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=" . $secret . "&response=" . $_POST['g-recaptcha-response'] . "&remoteip=" . $_SERVER['REMOTE_ADDR']); 
     $response = json_decode($response); 
     if (! $response->success) { 
      //return error 
     } 

     //code logic below 
} 

創建密鑰時提供的DIV代碼應該正確生成所有的在提交表單時,HTML可以通過您的PHP驗證進行處理。

1

這是一個解決方案。

  • 客戶端
<!DOCTYPE html> 
<html lang="en"> 
<head> 
    <meta charset="UTF-8"> 
    <title>reCaptcha</title> 

    <!--api link--> 
    <script src="https://www.google.com/recaptcha/api.js" async defer></script> 
    <!--call back function--> 
    <script> 
     function onSubmit(token) { 
      document.getElementById('reCaptchaForm').submit(); 
     } 
    </script> 
</head> 
<body> 
<div class="container"> 
    <form id="reCaptchaForm" action="signup.php" method="POST"> 
     <input type="text" placeholder="type anything"> 
     <!--Invisible reCaptcha configuration--> 
     <button class="g-recaptcha" data-sitekey="<your site key>" data-callback='onSubmit'>Submit</button> 
     <br/> 
    </form> 
</div> 
</body> 
</html> 
  • 服務器端驗證:創建signup.php文件
<?php 
//only run when form is submitted 
if(isset($_POST['g-recaptcha-response'])) { 
    $secretKey = '<your secret key>'; 
    $response = $_POST['g-recaptcha-response'];  
    $remoteIp = $_SERVER['REMOTE_ADDR']; 


    $reCaptchaValidationUrl = file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=$secretKey&response=$response&remoteip=$remoteIp"); 
    $result = json_decode($reCaptchaValidationUrl, TRUE); 

    //get response along side with all results 
    print_r($result); 

    if($result['success'] == 1) { 
     //True - What happens when user is verified 
     $userMessage = '<div>Success: you\'ve made it :)</div>'; 
    } else { 
     //False - What happens when user is not verified 
     $userMessage = '<div>Fail: please try again :(</div>'; 
    } 
} 
?> 
<!DOCTYPE html> 
<html> 
    <head> 
     <meta charset="UTF-8"> 
     <title>reCAPTCHA Response</title> 
    </head> 
    <body> 
     <?php 
      if(!empty($userMessage)) { 
       echo $userMessage; 
      } 
     ?> 
    </body> 
</html> 
4

我面臨同樣的問題了幾個小時,直到我終於理解了背後的邏輯這個「隱形驗證碼」! 你沒有得到迴應的原因很簡單,因爲有與g-recaptcha-response

這個textarea的只有後面臨的挑戰已經完成的響應串(這通常發生填充的標識和名稱的空textarea元素通常的驗證碼),但對於「隱形-驗證碼」,你必須顯式調用grecaptcha.execute();與「提交」按鈕,填入textarea的之後,你的表單自動提交(假設你已經綁定了您的callback功能提交)。

就我而言,我已經有PHP處理表單和驗證碼驗證,所以我決定堅持使用舊的「複選框」版本(至少直到它得到改善),因爲我意識到這將是非常惱人改變一切(表單提交邏輯,按鈕動作和JavaScript代碼)只是用來掩飾一個複選框!尤其是兩種方法幾乎相同!