Created
          February 5, 2016 08:25 
        
      - 
      
 - 
        
Save marat-dimaev/c72515357c679d1fe80b to your computer and use it in GitHub Desktop.  
    reCaptcha
  
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | <? | |
| /* | |
| reCaptcha представляет собой бесплатный сервис, который защищает ваш сайт от спама и злоупотреблений. | |
| // Подключение | |
| <?=reCaptcha::getHtml();?> | |
| // Проверка | |
| if(!reCaptcha::check()) { | |
| global $APPLICATION; | |
| $APPLICATION->ThrowException(reCaptcha::$msg); | |
| return false; | |
| } | |
| */ | |
| class reCaptcha | |
| { | |
| static public $publicKey = '6LcAbRcTAAAAAIlx2csyjIj4ToLSdzTU4MrPOiNM'; | |
| static private $secretKey = '6LcAbRcTAAAAAIb5EN38dR14KEMVsK5KAWXeEEMq'; | |
| static public $msg; | |
| static function addJs() { | |
| global $APPLICATION; | |
| $APPLICATION->AddHeadScript("https://www.google.com/recaptcha/api.js"); | |
| } | |
| static function getHtml() { | |
| self::addJs(); | |
| return '<div class="g-recaptcha" data-sitekey="'.self::$publicKey.'"></div>'; | |
| } | |
| static function check() { | |
| self::$msg = ''; | |
| if($_SERVER["REQUEST_METHOD"] == "POST") | |
| { | |
| $recaptcha=$_POST['g-recaptcha-response']; | |
| if(!empty($recaptcha)) | |
| { | |
| $google_url = "https://www.google.com/recaptcha/api/siteverify"; | |
| $secret = self::$secretKey; | |
| $ip = $_SERVER['REMOTE_ADDR']; | |
| $data = array( | |
| 'secret' => $secret, | |
| 'response' => $recaptcha, | |
| 'remoteip' => $ip | |
| ); | |
| $verify = curl_init(); | |
| curl_setopt($verify, CURLOPT_URL, $google_url); | |
| curl_setopt($verify, CURLOPT_POST, true); | |
| curl_setopt($verify, CURLOPT_POSTFIELDS, http_build_query($data)); | |
| curl_setopt($verify, CURLOPT_SSL_VERIFYPEER, false); | |
| curl_setopt($verify, CURLOPT_RETURNTRANSFER, true); | |
| $res = curl_exec($verify); | |
| $res = json_decode($res, true); | |
| //reCaptcha success check | |
| if($res['success']) | |
| { | |
| return true; | |
| } | |
| else | |
| { | |
| self::$msg="Ошибка! Проверка не пройдена."; | |
| } | |
| } | |
| else | |
| { | |
| self::$msg="Пожалуйста, укажите что вы не робот."; | |
| } | |
| } | |
| return false; | |
| } | |
| } | |
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment