2016-11-12 737 views
-1

我的reCaptcha無法驗證。無論如何,它都讓我通過。它發送電子郵件並重定向到感謝頁面,但不驗證reCaptcha。我錯過了什麼?這是我的PHP代碼。我沒有顯示它,但我確實有正確的代碼頭和形式標籤(從reCAPTCHA複製/粘貼)。感謝您提前提供任何幫助。ReCaptcha無法驗證

<?php 
if (isset($_POST['submit'])) { 
    $secret = 'MY SECRET KEY'; 
    $response = $_POST['g-recaptcha-response']; 
    $remoteip = $_SERVER['REMOTE_ADDR']; 

    $url = file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=$secret&response=$response&remoteip=$remoteip"); 
    $result = json_decode($url, TRUE); 
    if ($result['success'] == 1){ 
    } 
    } 

if(isset($_POST['submit'])) { 
    $emailbody = 'Name: '.$_POST['name']."\n" 
    .'Phone: '.$_POST['phone']."\n" 
    .'Email: '.$_POST['email']."\n" 
    .'Message: '.$_POST['message'];  
    mail('[email protected]', 'More Information', $emailbody); 
    header('location: thankyou.php'); 


exit(); 
    } 

?> 
+0

問題:你爲什麼用2x'f(isset($ _ POST ['submit']))'?這是什麼形式?也使用錯誤報告。最好的我可以提供 –

+0

無論我有什麼是我從視頻中得到的。我是一個新手。我肯定會接受更正。如果其他所有工作都正常,但我需要顯示錶格嗎? – Newsong80

+0

看來你試過這個http://stackoverflow.com/q/40400611/1415724是密切相關的。所以,我不知道這個表單是什麼樣的,或者你試圖獲得的密鑰是否有效。 –

回答

0

不過,您確認,您要驗證後處理,但if語句,在那裏你確定驗證是否是成功的......外面

該代碼可以改爲:

<?php 
if (isset($_POST['submit'])) { 
$secret = 'MY SECRET KEY'; 
$response = $_POST['g-recaptcha-response']; 
$remoteip = $_SERVER['REMOTE_ADDR']; 

$url = file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=$secret&response=$response&remoteip=$remoteip"); 
$result = json_decode($url, TRUE); 
if ($result['success'] == 1){ 

$emailbody = 'Name: '.$_POST['name']."\n" 
.'Phone: '.$_POST['phone']."\n" 
.'Email: '.$_POST['email']."\n" 
.'Message: '.$_POST['message'];  
mail('[email protected]', 'More Information', $emailbody); 
header('location: thankyou.php'); 
exit(); 
} else { 
// captcha failed 
} 
} 

?> 
+0

現在它正在驗證(我認爲),但不是重定向。 – Newsong80

+0

我插入「// captcha failed」,用「echo'captcha failed'替換掉';'如果頁面回顯出來,那麼驗證失敗。 – 2016-11-12 18:35:02

+0

現在它刷新頁面並在頁面頂部顯示回顯「captcha failed」,並且根本不顯示錶單。 – Newsong80