Skip to content

Instantly share code, notes, and snippets.

@fkumro
Created June 12, 2009 14:48
Show Gist options
  • Save fkumro/128671 to your computer and use it in GitHub Desktop.
Save fkumro/128671 to your computer and use it in GitHub Desktop.
<?php
class Test extends Controller
{
// store your private/public key
private $_recaptcha_private_key = 'YOUR PRIVATE KEY GOES HERE';
private $_recaptcha_public_key = 'YOUR PUBLIC KEY GOES HERE';
function __construct()
{
parent::__construct();
$this->load->helper('recaptcha');
}
function someAction()
{
$this->load->library('form_validation');
// set validation rule for the recaptcha response
$this->form_validation->set_rules('recaptcha_response_field', 'reCaptcha', 'required|callback_recaptcha_check');
if ($this->form_validation->run() === FALSE)
{
// store the recaptcha html code for the view
$data['recaptcha'] = recaptcha_get_html($this->_recaptcha_public_key);
// pass the data to the view
$this->load->view('someView', $data);
}
else
{
// party
}
}
function recaptcha_check($response)
{
// check to see if the recaptcha is correct
$resp = recaptcha_check_answer (
$this->_recaptcha_private_key,
$this->input->ip_address(),
$this->input->post('recaptcha_challenge_field'),
$this->input->post('recaptcha_response_field'));
if(!$resp->is_valid)
{
//reCaptcha is wrong
$this->form_validation->set_message('recaptcha_check', 'reCaptcha was wrong.');
return FALSE;
}
return TRUE;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment