Create captcha using Captcha helper in Codeigniter

In this post, I would like to show how to create captcha using captcha helper in codeigniter. Captcha solving improves the security of our login form. Or the form, from where we give permission to user to access their sensitive data after solving captcha.

Captcha can be generated by using inbuilt captcha helper of codeigniter. And also, with customizable options.
Follow me step by step i am going to show captcha helper implementation.

  1. Create view called captcha.php inside application/views.
<html>
<head>
<title>Codeigniter Captcha Helper Example</title>
</head>
<body>
<style>
h3{
  font-family: Verdana; 
  font-size: 18pt;         
  font-style: normal; 
  font-weight: bold; 
  color:red;
  text-align: center; 
}
table{
  font-family: Verdana; 
  color:black; 
  font-size: 12pt; 
  font-style: normal;
  font-weight: bold;
  text-align:left; 
  border-collapse: collapse; 
}
.error{
    color:red; 
    font-size: 11px;
}
</style>
<h3>Codeigniter Captcha Helper Example</h3>
 <?php echo form_open('captchacontroller/captcha',array('name' => 'captcha', 'method'=> 'post')); ?>
    <table align="center" cellpadding = "5">
        <tr>
            <td>User ID: </td>
            <td><input type="text" size="40px" name="userid" /></td>
            
        </tr>
        <tr>
            <td>Password: </td>
            <td><input type="text" size="40px" name="password" maxlength="10"/></td>
        </tr>
        <tr>
            <td>Captcha: </td>
            <td><p id="captImg"><?php echo $captchaImg; ?></p><br><input type="text" size="40px" name="captcha" value=""/></td>
        </tr>
        <tr>
            <td colspan="5" align="center">
            <input type="submit" value="Login" name="submit"/></td>
        </tr>
    </table>
<?php echo form_close();?>
</body>
</html>

2. Create controller file called CaptchaController.php.Load captcha helper and session library. Session library is for to set captcha word which is generated by captcha helper.  Put captcha functionality code inside this. Here is how code looks.

<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class CaptchaController extends CI_Controller {
    public function __construct()
    {
        parent::__construct();
        $this->load->library('session');
        $this->load->helper('captcha');
    }
    public function index() {
        $this->load->view('captcha');
    }
    public function captcha() {
        if($this->input->post('submit')){
            $userid = $this->input->post('userid');
            $password = $this->input->post('password');
            $captchaInput = $this->input->post('captcha');
            $sessionCaptcha = $this->session->userdata('captchaCode');
            if($captchaInput === $sessionCaptcha){
                echo 'Captcha verified.';
                /* Put your code */;
            }else{
                echo 'Captcha code did not verified, please try again.';
                /* Put your code */;
            }
        }
        else{
            $vals = array(
                'img_path'      => 'captcha_images/',  /* The path where the captcha images will be stored(required) */
                'img_url'       => base_url().'captcha_images/', /* The URL of captcha images (required) */
                /* 'font_path'     => './path/to/fonts/texb.ttf', */
                'img_width'     => '150', /* Width of captcha images */
                'img_height'    => 80, /* Height of captcha images */
                'word_length'   => 4, /* Number of chracter in captcha images */
                'font_size'     => 40,/* Font size of chracter in captcha images */
                /*'pool'          => '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ', */
                /* White background and border, black text and red grid */
                'colors'        => array(
                        'background' => array(255, 255, 255),
                        'border' => array(255, 255, 255),
                        'text' => array(0, 0, 0),
                        'grid' => array(255, 40, 255)
                )
        );
        $cap = create_captcha($vals);
        $data['captchaImg'] = $cap['image'];
        $this->session->unset_userdata('captchaCode');
        $this->session->set_userdata('captchaCode',$cap['word']);
        $this->load->view('captcha',$data);
        }   
    }  
}

Codeigniter Captcha Helper

 

Thank you for reading this post. we hope you like this Post, Please feel free to comment below, your suggestion and problems if you face – let us know. We’d love to help!

I am Hitesh from Jamshedpur (India). I have been working as a Web Application Developer from last 4+ years. I love diverse and attention catching web presence for a variety of users. Also I love to learn new things in Web Development.

Tags: , , ,