A Codeigniter library to validate Google Recaptcha.
A Codeigniter library to validate Google Recaptcha.
You need to copy config and library files into yout Codeiginter application.
Set config variables ( Google recaptcha sitekey and secret. You will be aboe to get those values form https://www.google.com/recaptcha/admin ).
Use the library in your controllers as below
<?php
class Form extends CI_Controller {
/**
* Index Controller
*
* @return void
*/
public function index()
{
$this->load->library('form_validation');
$this->form_validation->set_rules('g-recaptcha-response', 'recaptcha', 'trim|required|callback_validateCaptcha');
if ($this->form_validation->run() == FALSE){
//Form Error
}
else{
//Form Success
}
}
/**
* A callback function for validate captcha
*
* @param string $response
* @return boolean
*/
public function validateCaptcha($response){
$this->load->library('recaptcha');
if (!$this->recaptcha->Validate($response)){
$this->form_validation->set_message('validateCaptcha', 'The {field} field is incorrect. Please retry.');
return FALSE;
}
return TRUE;
}
This project is licensed under the MIT License - see the LICENSE.md file for details