2014-08-27 55 views
0

我敢肯定,這很簡單,但我不能看到它:)不能使用reCAPTCHA工作(甚至顯示!)

我想一個驗證碼加入到代碼爲現有網站有一點接觸形式。

我有谷歌的密鑰(當然在例子中被審查過),但我甚至無法得到它顯示CAPTCHA,更不用說測試過濾是否正常。

我已經添加了驗證碼代碼插入到頁面:

的index.html:

form name="f1" method="post" action="mail2.php" onsubmit="return verify();"> 
<p><label>Your Name <span>(*)</span></label><input type="text" name="name" /></p> 
<p><label>Your Email</label><input type="text" name="email" /></p> 
<p><label>Your Phone No: <span>(*)</span></label><input type="text" name="phone" /></p> 
<p><label>Other messages</label><textarea name="messages" rows="" cols=""></textarea> </p> 
<?php 
    require_once('recaptchalib.php'); 
    $publickey = "6LdrxxxxxxxxxxxxxxJr"; // you got this from the signup page 
     echo recaptcha_get_html($publickey); 
    ?> 

      <p style="padding-right:36px; float:right;"><input name="" type="image" src="images/submit_btn.png" /></p> 
      </form> 

而在mail2.php

<?php 
    require_once('recaptchalib.php'); 
    $privatekey = "6LxxxxxxxxxxxxxxxxxxqZYiH"; 
    $resp = recaptcha_check_answer ($privatekey, 
           $_SERVER["REMOTE_ADDR"], 
           $_POST["recaptcha_challenge_field"], 
           $_POST["recaptcha_response_field"]); 

    if (!$resp->is_valid) { 
    // What happens when the CAPTCHA was entered incorrectly 
    die ("The reCAPTCHA wasn't entered correctly. Go back and try it again." . 
     "(reCAPTCHA said: " . $resp->error . ")"); 
    } else { 
    // Your code here to handle a successful verification 



require_once 'mailer/class.phpmailer.php'; 

$mail = new PHPMailer(); 
----followed by the standard PHPMailer content---- 

索引頁只是顯示爲標準沒有顯示CAPTCHA部分,試圖發送電子郵件導致失敗並顯示一些PHP代碼。

我真的很感激,如果有人比我的頭腦更大吃&呼吸reCAPTCHA可以投下他們的眼睛,笑&指向我出錯的地方。 :)

希望受到教育,而不僅僅是一個修復。我的技能在於內容而不是編碼。

(我如何在編輯器中插入一個代碼塊?肯定沒有通過4位分別縮進每一行?)

非常感謝。

回答

-1

我可以幫忙,因爲我的工作比你更遠,但我也需要這方面的幫助。 首先,你需要這行代碼在HTML的頭部(</head>標記之前:

<script src="https://www.google.com/recaptcha/api.js?fallback=true" async defer></script> 

然後在你的HTML的身體,和裏面的某個地方你的形式(您<form> ... </form>標籤之間),你需要下面的代碼:

<div class="g-recaptcha" data-sitekey="....your site key here...."></div> 

然後在你的PHP處理文件(程序文件的形式職位,並接收您需要將以下代碼表單變量:

$response_string = $_POST['g-recaptcha-response']; 
$secret_key = "...put in your secret key here..."; 
$resp = file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=$secret_key&response=$response_string"); 

if($resp != '{ "success": true }') 
{ $problem = "failedreCAPTCHA"; 
    header("Location: http://www.yourwebsite.com/...pagetohandlefailedentry...php?problem=$problem"); 
    die('Sorry, you entered the wrong value in the Human CAPTCHA challenge. Please try again.'); 
} 

除了「if($ resp!='{」success「:true}')」部分外,這段代碼很好用。因爲從google填寫的變量$ resp得到的結果是「{」success「:true}」,這是一個JSON對象,我不知道如何使用PHP和JSON對象測試「true」。 所以...有人請幫忙。

+0

這並沒有真正提供問題的答案。如果您有新的問題,請點擊[問問題](http://stackoverflow.com/questions/ask)按鈕。您可以[添加賞金](http://stackoverflow.com/help/privileges/set-bounties)在足夠[聲譽](http:// stackoverflow。COM /幫助/什麼聲譽)。 – honk 2014-12-01 20:17:57

+0

歡迎來到StackOverflow。我可能會建議添加一個鏈接到你使用的任何資源。這樣可以幫助您將原始海報發送給一些更有幫助和完整的信息,因爲您提到不確定您答案的某個方面。 – philtune 2014-12-01 20:18:06

+0

我的回答是否回答了他的一些問題......他問道:「無法讓ReCAPTCHA正常工作(甚至不能顯示)」我提供的解決方案可以讓reCAPTCHA顯示。 我真的很失望,你「鳴」,你「philtune」。鄧肯裏德在幾個月前提出了一個問題,並沒有得到任何人的迴應。然後,我會一起去嘗試幫助,我會得到什麼回報?... 因此,您不是爲了回答問題而貢獻任何有用或有價值的東西,而只是嘲笑我。非常感謝。 – Brook 2014-12-01 20:33:06

0

這似乎是爲我工作,使用谷歌的新「沒有Captcha reCaptcha」。這很大程度上取決於Brook的答案,但包括ip,解析json和測試成功。

<?php 
    $gSecret = "...put in your secret key here..."; 
    $gResponse = $_POST['g-recaptcha-response']; 
    $gRemoteIp = $_SERVER['REMOTE_ADDR']; 
    $gResponse = file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=$gSecret&response=$gResponse&remoteip=$gRemoteIp"); 
    $resp = json_decode($gResponse); 

    if (!$resp->success) { 
    // What happens when the CAPTCHA was entered incorrectly 
    die ("The reCAPTCHA wasn't entered correctly. Go back and try it again." . 
     "(reCAPTCHA said: " . $resp->error-codes . ")"); 
    } else { 
    // Your code here to handle a successful verification 
    } 
?> 
相關問題