如何使用reCaptcha(2.0版本)来做网站验证码

来源:互联网 发布:微信现场抽奖软件 编辑:程序博客网 时间:2024/06/05 12:04

reCaptcha是Google开发的验证码工具。使用十分简单,本文介绍的是其2.0版本的使用方法。 

  1. 登陆你的Google账户,没有的话是用不了的。
  2. 在这里来申请一对keyhttps://www.google.com/recaptcha/admin ,如下图

    一个Google账户可以申请很多key,第一个label随便填,第二个是你的域名。我这里本地测试,直接输入localhost即可。
  3. 申请成功后可以看到两个key,一个是Site key,可以公开,一个是你自己的私key
  4. Client端设置:随便写一个HTML网页,需要带一个form,用来提交表单。在表单内想显示验证码的地方输入

    [html] view plain copy
    1. <span style="font-size:14px;">        <div class="g-recaptcha" data-sitekey="Yoursitekey"></div></span>  

    在</head>前输入

    [html] view plain copy
    1. <span style="font-size:14px;"><script src='https://www.google.com/recaptcha/api.js'></script></span>  

    如下:

    [html] view plain copy
    1. <span style="font-size:14px;"><!DOCTYPE html>  
    2. <html lang="en">  
    3. <head>  
    4.     <title></title>  
    5.     <meta charset="utf-8">  
    6.     <meta name="viewport" content="width=device-width, initial-scale=1">  
    7.       
    8.     <script src='https://www.google.com/recaptcha/api.js'></script>  
    9. </head>  
    10. <body>  
    11.   
    12.     <form action="confirm.php" method="post">  
    13.         <div class="g-recaptcha" data-sitekey="yoursitekey"></div>  
    14.           
    15.         <p><button class="btn btn-primary" type="submit">Register</button>  
    16.     </form>  
    17. </body>  
    18. </html>  
    19. </span>  


    这样的的主页将产生

    点击蓝色的方框即可开始验证

    如果sitekey与域名不匹配的话会显示


  5. 验证完毕并提交表单后,将会post一个g-recaptcha-response 值。这个值用来和Google服务器通信验证验证码是否正确。

    将这个值通过post方式发送到 https://www.google.com/recaptcha/api/siteverify 将获得一个jason格式结果,如下:

    即完成了验证。
    下面附上完整的验证代码

    [php] view plain copy
    1. <span style="font-size:14px;"><!DOCTYPE html>  
    2. <html lang="en">  
    3. <head>  
    4.     <title></title>  
    5.     <meta charset="utf-8">  
    6.     <meta name="viewport" content="width=device-width, initial-scale=1">  
    7.     <?php  
    8. function send_post($url$post_data)  
    9. {  
    10.     $postdata = http_build_query($post_data);  
    11.     $options = array(  
    12.         'http' => array(  
    13.             'method' => 'POST',  
    14.             'header' => 'Content-type:application/x-www-form-urlencoded',  
    15.             'content' => $postdata,  
    16.             'timeout' => 15 * 60 // 超时时间(单位:s)  
    17.         )  
    18.     );  
    19.     $context = stream_context_create($options);  
    20.     $result = file_get_contents($url, false, $context);  
    21.     return $result;  
    22. }  
    23.               
    24. $post_data = array(          
    25. 'secret' => 'yoursecretkey',          
    26. 'response' => $_POST["g-recaptcha-response"]    );  
    27.     $recaptcha_json_result = send_post('https://www.google.com/recaptcha/api/siteverify'$post_data);     
    28.  $recaptcha_result = json_decode($recaptcha_json_result);     
    29. //在这里处理返回的值   
    30. //var_dump($recaptcha_result);      
    31. ?>  
    32. </head>  
    33. <body>  
    34. echo $recaptcha_result;  
    35. </body>  
    36. </html>  
    37.   
    38.     </span>  
原创粉丝点击