How to use captcha in codeigniter

Rakesh Sharma      2 Comments  

How to use captcha in codeigniter using session

What is captcha :-

A program that use for human verification or program which protect websites against bots, spam etc.
Generally Captcha contain a image for human challenge on forms for human read and enter captcha image text. In codeigniter we can easily add captcha to website using captcha helper class. it’s provided by codeigniter default. In this article how to use captcha in codeigniter We are using session and a callback function to validate a captcha field/words. There is also alternate you can also manage captcha with database but using session is a simple way to manage. Below i am sharing an example how to use captcha in codeigniter website forms.

1. create a directory on root with named “captcha”(must be writable).

2. create a controller with named “captcha.php” under “application/controllers/” and add below code.

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Captcha extends CI_Controller {
  public function index() {
    // loading captcha helper
    //validating form fields
    $this->form_validation->set_rules('username', 'Email Address', 'required');
    $this->form_validation->set_rules('user_password', 'Password', 'required');
    $this->form_validation->set_rules('userCaptcha', 'Captcha', 'required|callback_check_captcha');
    $userCaptcha = $this->input->post('userCaptcha');
    if ($this->form_validation->run() == false){
      // numeric random number for captcha
      $random_number = substr(number_format(time() * rand(),0,'',''),0,6);
      // setting up captcha config
      $vals = array(
             'word' => $random_number,
             'img_path' => './captcha/',
             'img_url' => base_url().'captcha/',
             'img_width' => 140,
             'img_height' => 32,
             'expiration' => 7200
      $data['captcha'] = create_captcha($vals);
      $this->load->view('captcha', $data);
    else {
      // do your stuff here.
      echo 'I m here clered all validations';
  public function check_captcha($str){
    $word = $this->session->userdata('captchaWord');
    if(strcmp(strtoupper($str),strtoupper($word)) == 0){
      return true;
      $this->form_validation->set_message('check_captcha', 'Please enter correct words!');
      return false;

3. create a view file with named “captcha.php” under “application/views/” and add below code.

<title>Adding a Captcha!</title>
<h1>Captcha Example</h1>
<?php echo form_open('captcha'); ?>
<div class="formSignIn" >
  <div class="form-group">
    <input autocomplete="off" type="text" id="username" name="username" placeholder="User Email" value="<?php if(!empty($username)){ echo $username;} ?>" />
    <span class="required-server"><?php echo form_error('username','<p style="color:#F83A18">','</p>'); ?></span> </div>
  <div class="form-group">
    <input autocomplete="off" type="password" id="user_password" name="user_password" placeholder="User Password" value="" />
    <span class="required-server"><?php echo form_error('user_password','<p style="color:#F83A18">','</p>'); ?></span> </div>
  <div class="form-group">
    <label for="captcha"><?php echo $captcha['image']; ?></label>
    <input type="text" autocomplete="off" name="userCaptcha" placeholder="Enter above text" value="<?php if(!empty($userCaptcha)){ echo $userCaptcha;} ?>" />
    <span class="required-server"><?php echo form_error('userCaptcha','<p style="color:#F83A18">','</p>'); ?></span> </div>
  <div class="form-group">
    <input type="submit" class="btn btn-success" value="Sign In" name="" />
<?php echo form_close(); ?>

How to use captcha in codeigniter

Run your controller and see you are all done with How to use captcha in codeigniter.

  • Sanjay Raj

    Good Work


    good one