Every time I make a new website with a user registration, I usually end up using a reCaptcha somewhere in the process. A while ago, I discovered a reCaptcha library on the Codeigniter forums. And since then, Ive modified it a little bit to work with Codeigniter 2.0and have placed it on Github where everyone can access it. Below is just an example of the Controller (included in the repository) so you can see how it all comes together.
<?php/**
* Sample recaptcha controller implementation
*
*/class Recaptcha_Controller extends Controller{
public function __construct()
{
parent::Controller();
$this->load->library('recaptcha');
$this->load->library('form_validation');
$this->load->helper('form');
$this->lang->load('recaptcha');
}
public function index()
{
$data = array('mainData' => '',
'recaptcha' => $this->recaptcha->get_html()
);
$this->load->view('recaptcha_view', $data);
}
public function submit()
{
$ipAddress = $this->input->ip_address();
$challengeField = $this->input->post('recaptcha_challenge_field');
$responseField = $this->input->post('recaptcha_response_field');
if ($this->recaptcha->check_answer($ipAddress, $challengeField, $responseField))
{
// success!!echo 'Success!';
// redirect here or do what you want
}
else
{
// failure!!// redirect back to the recaptcha page
redirect(site_url('recaptcha_controller'));
}
}
}
?>