Click here to Skip to main content
12,886,364 members (45,296 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


8 bookmarked
Posted 29 Jul 2008

Captcha Plug-in in CodeIgniter

, 29 Jul 2008 CPOL
Rate this:
Please Sign up or sign in to vote.
How to use thr captcha plugin in CodeIgniter.


CAPTCHA - Completely Automated Public Turning test to tell Computers and Humans Apart. It is used in web pages to protect your page from automatic posting from outside of the site. The CodeIgniter framework has a plugin named "captcha_pi.php" in the plugins folder. Here I've demonstrated an example of how to use this captcha plug-in in your website's built-in CodeIgniter framework.

Let's Start

Make a controller named "captcha.php" and place it in the "controllers" folder. This controller is as follows:

class Captcha extends Controller
  public function __construct()



  public function index()
      $captcha = $this->captcha_model->generateCaptcha();

      $_SESSION['captchaWord'] = $captcha['word'];

      $data['captcha'] = $captcha;

      $this->load->view('show_view', $data);
      //check captcha matches
                    $_POST['confirmCaptcha']) == 0)

Here, I've made a model named "captcha_model" that has a method generateCaptcha(), which generates the captcha using the CodeIgniter captcha plug-in and returns an array. The captcha word is saved in the session, and after submitting the form, this session word is compared with the posted word for matching.

If it matches, which is in the condition (strcasecmp($_SESSION['captchaWord'], $_POST['confirmCaptcha']) == 0), then success is shown. Otherwise, failure is shown. Here, I've used the strcasecmp() function of PHP to compare the word case-insensitively. If you want user to enter the captcha words as they are shown (i.e., capital letter in capital and small letter in small), then you can use the strcmp() function.

The captcha_model.php is as follows:

class Captcha_model extends Model
  private $vals = array();

  private $baseUrl  = 'http://localhost/captchademo/';
  private $basePath = 'D:/apache2triad/htdocs/captchademo/';

  private $captchaImagePath = 'tmp/captcha/';
  private $captchaImageUrl  = 'tmp/captcha/';
  private $captchaFontPath  = 'c:/windows/fonts/verdana.ttf';

  public function __construct($configVal = array())


      $this->vals = array(
                 'word'          => '',
                 'word_length'   => 6,
                 'img_path'      => $this->basePath
                                 .  $this->captchaImagePath,
                 'img_url'       => $this->baseUrl
                                 .  $this->captchaImageUrl,
                 'font_path'     => $this->captchaFontPath,
                 'img_width'     => '150',
                 'img_height'    => 50,
                 'expiration'    => 3600

   * initializes the variables
   * @author    Mohammad Jahedur Rahman
   * @access    public
   * @param     array     config
  public function initialize ($configVal = array())
    $this->vals = $configVal;
  } //end function initialize


   * generate the captcha
   * @author     Mohammad Jahedur Rahman
   * @access     public
   * @return     array
  public function generateCaptcha ()
    $cap = create_captcha($this->vals);

    return $cap;
  } //end function generateCaptcha

You have to change the $baseUrl, $basePath, $captchaImagePath, $captchaImageUrl, and $captchaFontPath etc. To store the captcha images, at first, I created a folder "tmp" and a sub-folder "captcha" in the main directory where index.php exists. If you are running the code on the server, be sure these two folders are given write permission.

By default, the captcha plug-in generates 8 letter words. But, I wanted to vary this length. So, I made a little change in this plug-in. In line 156 of "captcha_pi.php":

$defaults = array('word'       => '',    'img_path' => '',
                  'img_url'    => '',    'img_width' => '150',
                  'img_height' => '30',  'font_path' => '',
                  'expiration' => 7200);  

was changed to:

$defaults = array('word'      => '',    'word_length' => 8,
                  'img_path'  => '',    'img_url'     => '',
                  'img_width' => '150', 'img_height'  => '30',
                  'font_path' => '',    'expiration'  => 7200);

Next, line 226:

for ($i = 0; $i < 8; $i++)

was changed to:

for ($i = 0; $i < $word_length; $i++)

Now, if you pass word length as a parameter, then it will generate a captcha image of the word length you have specified.


Points of Interest

Although it is easier to use the captcha plug-in in CodeIgniter, I suggest you use ReCaptcha.

You can read my blog here.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Jahedur Rahman Chowdhury
Software Developer (Senior)
Bangladesh Bangladesh
Software Engineer, Bangladesh.

You may also be interested in...


Comments and Discussions

Questioncaptcha not working Pin
RItzz D ViajEro24-Jul-14 21:41
memberRItzz D ViajEro24-Jul-14 21:41 
GeneralMy vote of 5 Pin
Md. Marufuzzaman25-Jul-11 8:54
mvpMd. Marufuzzaman25-Jul-11 8:54 
GeneralMy vote of 4 Pin
Sakthi Prakash D23-Sep-10 21:16
memberSakthi Prakash D23-Sep-10 21:16 
GeneralCaptcha not displayed - image path not valid directory Pin
Anbarasan kanagaraj24-Aug-10 5:29
memberAnbarasan kanagaraj24-Aug-10 5:29 

I have the same problem.
The captcha image not displayed.

In the plugin- captcha_pi.php file i put down the echo for image path ($img_path). but it did not displayed the message. i put down this echo in is_dir($img_path) if condition in captcha_pi.php file in plugins module.

it shows echo message. then i came to know that, the image path is not directory.

would you please tell me why is occur.

my image path is,


but why it shows it not directory.I gave file permission for captcha to 777

please suggest me how fix this.
Generalcaptcha image path not valid directory Pin
Anbarasan kanagaraj24-Aug-10 5:27
memberAnbarasan kanagaraj24-Aug-10 5:27 
GeneralMy vote of 5 Pin
bharathi22-Jul-10 23:42
memberbharathi22-Jul-10 23:42 
GeneralThank You Pin
aataqwa27-Aug-08 20:16
memberaataqwa27-Aug-08 20:16 
GeneralRe: Thank You Pin
Mohammad Jahedur Rahman27-Aug-08 20:27
memberMohammad Jahedur Rahman27-Aug-08 20:27 
Generalimage no displya Pin
aataqwa27-Aug-08 3:58
memberaataqwa27-Aug-08 3:58 
AnswerRe: image no displya Pin
Mohammad Jahedur Rahman27-Aug-08 4:20
memberMohammad Jahedur Rahman27-Aug-08 4:20 
GeneralHelp Me.. Pin
aataqwa27-Aug-08 2:58
memberaataqwa27-Aug-08 2:58 
AnswerRe: Help Me.. Pin
Mohammad Jahedur Rahman27-Aug-08 3:25
memberMohammad Jahedur Rahman27-Aug-08 3:25 
GeneralIt looks nice Pin
Mohammad Dayyan30-Jul-08 11:33
memberMohammad Dayyan30-Jul-08 11:33 
GeneralRe: It looks nice Pin
Mohammad Jahedur Rahman2-Aug-08 19:50
memberMohammad Jahedur Rahman2-Aug-08 19:50 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.170424.1 | Last Updated 29 Jul 2008
Article Copyright 2008 by Jahedur Rahman Chowdhury
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid