CodeIgniter Captcha

CAPTCHA ("Completely Automated Public Turing test to tell Computers and Humans Apart") is a type of challenge–response test used in computing to determine whether the user is human or not. CAPTCHA mostly used in the web application to protect the website from getting spammed. In a CAPTCHA Verification, the respondent is presented with a picture (or "challenge") of words or characters, and the respondent must correctly type out those characters in order to proceed.

The CAPTCHA Helper file contains functions that assist in creating CAPTCHA images.

In this tutorial, we'll see how to implement CAPTCHA in CodeIgniter.

Loading CAPTCHA Helper


Adding a Database

we'll need to add the information returned from create_captcha() to your database. Below the table prototype:

CREATE TABLE captcha (
        captcha_id bigint(13) unsigned NOT NULL auto_increment,
        captcha_time int(10) unsigned NOT NULL,
        ip_address varchar(45) NOT NULL,
        word varchar(20) NOT NULL,
        PRIMARY KEY `captcha_id` (`captcha_id`),
        KEY `word` (`word`)

Using a text editor, create a controller called Mycaptcha.php. In it, place this code and save it to your application/controllers/ directory:

   class Mycaptcha extends CI_Controller {
      public function __construct() { 
         $this->load->helper(array('form', 'url', 'captcha', 'html')); 
      public function index() {
         /* Load form validation library */ 
	 /* Validation rule */
	 $this->form_validation->set_rules('captcha', 'Captcha', 'required|callback_checkCaptcha');	 
         if ($this->form_validation->run() == FALSE) {
		        $arr = array(
			            'img_path'    => './captcha/',
				    'img_url'     => base_url().'/captcha/',
				    'word_length' => 4
			 $captcha = create_captcha($arr);
			 $data = array(
					'captcha_time'  => $captcha['time'],
					'ip_address'    => $this->input->ip_address(),
					'word'          => $captcha['word']
			$query = $this->db->insert_string('captcha', $data);

			$this->load->view('captcha_form', compact('captcha')); 
         else {
		    $success = "Captcha has been successfully verified!";
                    $this->load->view('captcha_form', compact('success')); 

	public function checkCaptcha($captcha)
		   // First, delete old captchas
		   $expiration = time() - 7200; // Two hour limit
		   $this->db->where('captcha_time < ', $expiration)->delete('captcha');
		   // Then see if a captcha exists:
		   $sql = 'SELECT COUNT(*) AS count FROM captcha WHERE word = ? AND ip_address = ? AND captcha_time > ?';
		   $binds = array($captcha, $this->input->ip_address(), $expiration);
		   $query = $this->db->query($sql, $binds);
		   $row = $query->row();
		   if ($row->count ==  0) {
			  $this->form_validation->set_message('checkCaptcha','The code you entered is incorrect');
			  return FALSE;
			  return TRUE;

Note: create a captcha directory under root folder.

Using a text editor, create a form called captcha_form.php. In it, place this code and save it to your application/views/ directory:

<!DOCTYPE html>
<html lang="en">
<link rel="stylesheet" href="" />
<script src=""></script>
<script src=""></script>
<div class="container">
  <div class="col-md-2"></div>
     <div class="col-md-8" style="margin-top:20px">
		   echo form_open('mycaptcha/index');
		   echo validation_errors();
		   if (isset($success)) {
		     echo '<p>'.$success.'</p>';
		  } else {
		<div class="form-group">
			<label for="image">Image:</label>
			<?php echo img('captcha/'.$captcha['filename']); ?>
		<div class="form-group">
			<label for="captcha">Type the code shown:</label>
			<input type="text" id="captcha" name="captcha" />
		<button type="submit" class="btn btn-success">Submit</button>
		echo form_close(); 
	 <div class="col-md-2"></div>

To try captcha form, visit your site using a URL similar to this one:


codeigniter captcha codeigniter captcha2 codeigniter captcha3