2014-12-03 71 views
12

https://developers.google.com/recaptcha/docs/verify與驗證碼v2和表單提交

if(isset($_POST['submit'])){ 
$recaptchaResponse = $_POST['g-recaptcha-response']; 
$secretKey = 'MYKEY'; 
$request = file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=".$secretKey."&response=".$recaptchaResponse); 

    if(!strstr($request,"false")){ 
echo '<div class="notification error clearfix"><p><strong>Attention!</strong> You didnt complete the captcha.</p></div>'; 
exit(); 

那麼php文件郵件的休息形式掙扎,但它只是發送反正就算你不完成的ReCaptcha。基本上,如果JSON返回一個錯誤,我希望在這難道不發送,並會顯示錯誤

而且,這裏是從頁面的形式,如果有幫助,我已經大概做了錯事有太多...

<form method="POST" action="post.php" name="contactform" id="contactform" class="container"> 

      <fieldset> 
       <div class="form-field grid-half"> 
        <label for="name">Name</label> 
        <span><input type="text" name="name" id="name" /></span> 
       </div> 
       <div class="form-field grid-half"> 
        <label for="email">Email</label> 
        <span><input type="email" name="email" id="email" /></span> 
       </div> 
       <div class="form-field grid-full"> 
        <label for="message">Message</label> 
        <span><textarea name="message" id="message"></textarea></span> 
       </div>     
       <div class="form-field grid-full"> 
         <div class="g-recaptcha" data-sitekey="MYKEY"></div> 
       </div> 
      </fieldset> 
      <div class="form-click grid-full"> 
       <span><input type="submit" name="submit" value="Submit" id="submit" /></span> 
      </div> 

      <div id="alert" class="grid-full"></div> 
     </form>  
+2

爲什麼要使用Captcha?在我們公司,我們進行了一項研究,發現約有30%的人只需填寫驗證碼即可。也許看看蜜罐技術加上一些良好的服務器端驗證和技術,比如跟蹤用戶完成textarea的總體鏈接的表單和內容所需的時間。如果你這樣做,你將不再需要驗證碼,用戶會很高興。閱讀它可能會有所幫助:http:// solutionfactor。net/blog/2014/02/01/honeypot-technique-fast-easy-spam-prevention/ – w3shivers 2014-12-03 06:48:54

回答

15

我有時也會發現,這取決於PHP版本/配置,訪問對象直接將無法正常工作,所以使用json_decode()

$response=file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=YOUR_SITE_KEY&response=".$captcha."&remoteip=".$_SERVER['REMOTE_ADDR']); 
$obj = json_decode($response); 
if($obj->success == true) 
{ 
    //passes test 
} 
else 
{ 
    //error handling 
} 
+0

要清楚,'YOUR_SITE_KEY'應該是「祕密密鑰」*不是*「網站密鑰」和'$ captcha'是'$ _POST ['g-recaptcha-response']' – mpen 2018-02-19 01:46:24

-4
if (response == true) { 
    mail(); 
} else { 
    echo "error"; 
} 
1

粘貼到您的HTML模板結束標記之前這個片段:

<script src='https://www.google.com/recaptcha/api.js'></script> 

在的結尾,你想要的reCAPTCHA小工具出現粘貼此片段:

<div class="g-recaptcha" data-sitekey="YOUR_SITE_KEY"></div> 

實施例:

<html> 
    <head> 
    <title>Google recapcha demo - Codeforgeek</title> 
    <script src='https://www.google.com/recaptcha/api.js'></script> 
    </head> 
    <body> 
    <h1>Google reCAPTHA Demo</h1> 
    <form id="comment_form" action="form.php" method="post"> 
     <input type="email" placeholder="Type your email" size="40"><br><br> 
     <textarea name="comment" rows="8" cols="39"></textarea><br><br> 
     <input type="submit" name="submit" value="Post comment"><br><br> 
     <div class="g-recaptcha" data-sitekey="YOUR_SITE_KEY"></div> 
    </form> 
    </body> 
</html> 

form.php的

<?php 

     $email;$comment;$captcha; 
     if(isset($_POST['email'])){ 
      $email=$_POST['email']; 
     }if(isset($_POST['comment'])){ 
      $email=$_POST['comment']; 
     }if(isset($_POST['g-recaptcha-response'])){ 
      $captcha=$_POST['g-recaptcha-response']; 
     } 
     if(!$captcha){ 
      echo '<h2>Please check the the captcha form.</h2>'; 
      exit; 
     } 
     $response=file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=YOUR_SECRET_KEY&response=".$captcha."&remoteip=".$_SERVER['REMOTE_ADDR']); 
     if($response.success==true)    
     { 
      echo '<h2>Thanks for posting comment.</h2>'; 
     } 
?> 

SORCE:

+1

我發現這個解決方案依賴於PHP版本和一些配置(它並不總是可以控制某些共享服務器)。爲了補救,使用'$ obj = json_decode($ response); \t \t if($ obj - > {'success'} == true)' – 2016-01-08 14:00:49

+1

它對我很好。謝謝:) – 2018-02-25 12:56:47

8

使用curl代替file_get_contents(你應該和我一樣,想file_get_contents在服務器設置被禁用)

$post_data = "secret=__your_secret_key__&response=". 
    $_POST['g-recaptcha-response']."&remoteip=".$_SERVER['REMOTE_ADDR'] ; 

$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL, "https://www.google.com/recaptcha/api/siteverify"); 
curl_setopt($ch, CURLOPT_POST, true); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt($ch, CURLOPT_HTTPHEADER, 
    array('Content-Type: application/x-www-form-urlencoded; charset=utf-8', 
    'Content-Length: ' . strlen($post_data))); 
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data); 
$googresp = curl_exec($ch);  
$decgoogresp = json_decode($googresp); 
curl_close($ch); 

if ($decgoogresp->success == true) 
    { 
    // Success 
    } 
0

首先,這裏已經給出的答案是絕對足夠的。 這就是說,我只是想包含這個函數,將這些答案包裝成一個稍微方便一點的方法,這樣你可以將它們扔進你的函數庫,並且在谷歌改變某些東西之前幾乎忘記了它。請享用!

//Put these two functions into your function library------- 
function CaptchaVerify($apiSecret) 
{ 
    //Returns -1 if no captcha response was in post(user did not submit form) 
    //Returns 0 if the verification fails 
    //Returns 1 if the verification succeeds 
    $captcha = isset($_POST['g-recaptcha-response'])? "&response=".$_POST['g-recaptcha-response']:''; 
    if($captcha != '') 
    { 
     $verifyUrl = "https://www.google.com/recaptcha/api/siteverify"; 
     $apiSecret = "?secret=".$apiSecret; 
     $remoteip = "&remoteip=".$_SERVER['REMOTE_ADDR']; 
     $response=file_get_contents($verifyUrl.$apiSecret.$captcha.$remoteip); 
     $obj = json_decode($response); 
     return (integer)$obj->success;   
    } 
    return -1; 
} 
function MyCaptchaVerify() 
{ 
    $apiSecret = "PUT YOUR CAPTCHA SECRET HERE"; 
    return CaptchaVerify($apiSecret); 
} 
//------------------------------------------------------- 
//Example usage in your form 
switch(MyCaptchaVerify()) 
{ 
    case -1:echo "The form has not been submitted yet(First Load).<br>";break; 
    case 0:echo "The captcha verification failed.<br>"; break; 
    case 1:echo "The captcha verification succeeded.<br>"; break; 
} 
1

我更喜歡file_get_contents的cURL示例,因爲它爲錯誤日誌記錄等提供了更多選項。有些人發現cURL很艱鉅。對於那些用戶來說,Guzzle是一個非常好的選擇。

+1

這不是爲這個問題提供一個答案。一旦你有足夠的[聲譽](https://stackoverflow.com/help/whats-reputation),你將可以[對任何帖子發表評論](https://stackoverflow.com/help/privileges/comment);相反,[提供不需要提問者澄清的答案](https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-c​​an- I-DO-代替)。 - [來自評論](/ review/low-quality-posts/18932729) – 2018-02-26 03:48:06