2017-04-22 300 views
0


我在使用Google captcha可能會在php聯繫表單中遇到問題。
我跟着谷歌官方指南和我寫的代碼似乎是正確的,但我總是收到錯誤,驗證碼爲,錯誤代碼爲Google reCAPTCHA:錯誤驗證碼錯誤

客戶端代碼:

<div class="row"> 
    <div class="12u"> 
     <form id="mailform" method="post" action="website_mailer.php" novalidate="novalidate"> 
      <div> 
       <div class="row half"> 
        <div class="6u"> 
         <input type="text" name="name" id="name" placeholder="Name" /> 
        </div> 
        <div class="6u"> 
         <input type="text" name="email" id="email" placeholder="Email" /> 
        </div> 
       </div> 
       <div class="row half"> 
        <div class="12u"> 
         <input type="text" name="subject" id="subject" placeholder="Subject" /> 
         <input type="text" class="myantispam" name="leaveblank"> 
         <input type="text" class="myantispam" name="dontchange" value="http://" > 
        </div> 
       </div> 
       <div class="row half"> 
        <div class="12u"> 
         <textarea name="message" id="message" placeholder="Message"></textarea> 
        </div> 
       </div> 
       <div class="row half"> 
        <div class="4u"> 
         <div class="g-recaptcha" data-sitekey="xxxx"></div> 
        </div> 
        <div class="8u"> 
         <a href="#" class="button form-button-submit">Send Message</a> 
         <a href="#" class="button button-alt form-button-reset">Clear Form</a> 
        </div> 
       </div> 
      </div> 
     </form> 
    </div> 
</div> 

服務器端代碼(PHP):

<?php 
require_once('recaptchalib.php'); 

$privatekey = "xxx"; 
$resp = recaptcha_check_answer ($privatekey, 
           $_SERVER["REMOTE_ADDR"], 
           $_POST["recaptcha_challenge_field"], 
           $_POST["recaptcha_response_field"]); 

if (!$resp->is_valid) { 
     die ("The reCAPTCHA wasn't entered correctly. Go back and try it again." . 
     "(reCAPTCHA said: " . $resp->error . ")"); 
    } else { 
     /* proper code */ 
    } 
?> 

您能否提供我一個解決方案嗎?我無法找到解決方案。

謝謝!

回答

0

我使用下面的代碼解決了問題

$response=json_decode(file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=SECRET_KEY_HERE&response=".$captcha."&remoteip=".$_SERVER['REMOTE_ADDR']), true); 
if($response['success'] == false){ 
    //Captcha incorrect 
} 
else { 
    //Success code here 
} 

,而不是

<?php 
require_once('recaptchalib.php'); 

$privatekey = "xxx"; 
$resp = recaptcha_check_answer ($privatekey, 
           $_SERVER["REMOTE_ADDR"], 
           $_POST["recaptcha_challenge_field"], 
           $_POST["recaptcha_response_field"]); 

if (!$resp->is_valid) { 
     die ("The reCAPTCHA wasn't entered correctly. Go back and try it again." . 
     "(reCAPTCHA said: " . $resp->error . ")"); 
    } else { 
     /* proper code */ 
    } 
?> 

感謝Locke Donohoe誰建議答案here