Last active
January 9, 2017 23:04
-
-
Save jlamim/4b0af76cdde4aad8375d78a05ac9f447 to your computer and use it in GitHub Desktop.
Google reCaptcha com CodeIgniter - Controller Welcome
This file contains 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
<?php | |
defined('BASEPATH') OR exit('No direct script access allowed'); | |
class Welcome extends CI_Controller { | |
public function index() | |
{ | |
// Define as chaves de acesso à API | |
$dados['siteKey'] = 'SUA_SITE_KEY'; | |
$dados['secretKey'] = 'SUA_SECRET_KEY'; | |
// Verifica se o campo com o código do reCaptcha foi enviado para o servidor | |
if ($this->input->post('g-recaptcha-response')) { | |
// Faz a instanciação e verificação do reCaptcha | |
$recaptcha = new \ReCaptcha\ReCaptcha($dados['secretKey']); | |
$resp = $recaptcha->verify($this->input->post('g-recaptcha-response'), $_SERVER['REMOTE_ADDR']); | |
// Verifica se o reCaptcha foi preenchido com sucesso | |
if ($resp->isSuccess()) { | |
echo "reCaptcha validado com sucesso."; | |
} else { | |
echo "Problemas ao validar o reCaptcha: ". implode(', ', $resp->getErrorCodes()); | |
} | |
} else { | |
// Carrega a view para exibição do reCaptcha no browser | |
$this->load->view('welcome_message', $dados); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment