验证码之google的reCAPTCHA使用

天热了,各种选秀节目不断,互联网活动上的各种选秀节目也不少。有了选秀,就会有投票,票数的多少导致的名次先后之争直接牵扯到选手的利益。于是,各种刷票产业应运而生。

笔者用过各种防刷票的方法来杜绝,基本都无用。每次活动经历刷票工具的攻城拔寨后,票数暴涨。

最近采用了google的reCAPTCHA验证码机制,刷票基本杜绝。具体使用方法记录如下,已被后用。

1、首先申请API Keys

URL: https://www.google.com/recaptcha/admin/create

2、非插件方式

页面端代码方式一:直接页面显示


  


页面端代码方式二:AJAX


    
    
      
      
      
      
    
    
      
      
用户输入验证:ajax请求

URL: http://www.google.com/recaptcha/api/verify

Parameters (sent via POST)  
privatekey (required) Your private key
remoteip (required) The IP address of the user who solved the CAPTCHA.
challenge (required) The value of "recaptcha_challenge_field" sent via the form
response (required) The value of "recaptcha_response_field" sent via the form
Recaptcha.get_challenge() 获取challenge;

Recaptcha.get_response() 获取 response.

返回:

Line 1 "true" or "false". True if the reCAPTCHA was successful
Line 2 if Line 1 is false, then this string will be an error code. reCAPTCHA can display the error to the user (through theerror parameter of www.google.com/recaptcha/api/challenge). Implementations should not depend on error code names, as they may change in the future.

Example: If your response looks like this:
false
incorrect-captcha-sol

... you can add '&error=incorrect-captcha-sol' to the challenge request URL, and the user will get an error code.

3、使用PHP的插件方式

客户端:


     
      

      

服务器端:

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
  }
?>

文档地址:https://developers.google.com/recaptcha/intro?hl=zh-CN


你可能感兴趣的:(验证码之google的reCAPTCHA使用)