reCaptcha

Klient – Kod HTML

-- head
<script src='https://www.google.com/recaptcha/api.js'></script>

-- form - przycisk reCaptcha
<div class="g-recaptcha" data-sitekey="62LczhlYUAAAAAAYW6nsSPsXJ-mulLH6w7TxFH4Pa"></div>

Serwer – kod PHP

if($_POST['send'])
{           
 $response = $_POST['g-recaptcha-response'];

 if($response) {

 $url = "https://www.google.com/recaptcha/api/siteverify";
 $secret = "62LczhlYUAAAAADTnGOz8GRl9c66Rwn4GDSVsChAg";
 $remoteip = $_SERVER['REMOTE_ADDR'];
 $params = [
            'secret'   => $secret, 
            'remoteip' => $remoteip, 
            'response' => $response
           ];      
  
 $post_params = http_build_query($params);     
   
 $ch = curl_init();        
 curl_setopt($ch, CURLOPT_URL, $url);
 curl_setopt($ch, CURLOPT_HEADER, false);
 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
 curl_setopt($ch, CURLOPT_POST, true);
 curl_setopt($ch, CURLOPT_POSTFIELDS, $post_params);       
 $response = curl_exec($ch);
 curl_close($ch);

 $resp_tab = json_decode($response, true);  
  
 if( $resp_tab['success'] )
 {
   //Przetwarzanie i wysłanie formularza
   ...
   $info = "Formularz wysłany";
 } 
 else {
   $info = "Nieprawidłowa weryfikacja testu w Google. 
            Kod błęd: " .$resp_tab['error-codes'][0];
  }
 } 
 else {
     $info = "Błędny wybór lub nie wypełniłeś chptcha."; 
 }
 $smarty->assign('info', $info);
}

Dodaj komentarz