Advertisement
ben2613

mod_recaptcha_v2.php

Jan 25th, 2015
290
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.50 KB | None | 0 0
  1. <?php
  2. /*
  3. mod_recaptcha_v2.php
  4. author/LINE: ben2613
  5.  
  6. Try to use the Google ReCaptcha v2 referring to the mod_recaptcha.php
  7. the recaptchalib.php below can be found at
  8. https://github.com/google/ReCAPTCHA/tree/master/php
  9.  
  10. You can register at
  11. https://www.google.com/recaptcha/admin
  12. */
  13. require_once('recaptchalib.php'); // reCAPTCHA PHP Library
  14.  
  15. class mod_recaptcha_v2{
  16.     var $KEY_PUBLIC, $KEY_PRIVATE;
  17.     var $reCaptcha;
  18.  
  19.     function mod_recaptcha_v2(){
  20.         $this->KEY_PUBLIC = ''; // Public Key of this site
  21.         $this->KEY_PRIVATE = ''; // Private Key of this site
  22.         $this->reCaptcha = new ReCaptcha($this->KEY_PRIVATE);
  23.     }
  24.  
  25.     function getModuleName(){
  26.         return 'mod_recaptcha_v2 : reCAPTCHA v2';
  27.     }
  28.  
  29.     function getModuleVersionInfo(){
  30.         return '0.Alpha.0 (v150126)';
  31.     }
  32.  
  33.     function autoHookHead(string &$head, int $isReply){
  34.         $head.="<script src='https://www.google.com/recaptcha/api.js?hl=zh-TW'></script>";
  35.     }
  36.  
  37.     /* 在頁面附加 reCAPTCHA 功能 */
  38.     function autoHookPostForm(&$txt){
  39.         $txt .= '<tr><th class="Form_bg">驗證</th><td>'.'<div class="g-recaptcha" data-sitekey="'.$this->KEY_PUBLIC.'"></div>'.'</td></tr>\n';
  40.     }
  41.  
  42.     /* 在接收到送出要求後馬上檢查是否正確 */
  43.     function autoHookRegistBegin(&$name, &$email, &$sub, &$com, $upfileInfo, $accessInfo){
  44.         $resp = $this->reCaptcha->verifyResponse($_SERVER['REMOTE_ADDR'], $_POST['g-recaptcha-response']);
  45.         if($resp == null || !$resp->success){ error('reCAPTCHA failed!You are not acting like a human!'); } // 檢查
  46.     }
  47. }
  48. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement