2014-12-03 121 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