Create Captcha using Captcha Helper in Codeigniter

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.
<title>Codeigniter Captcha Helper Example</title>
font-family: Verdana;
font-size: 18pt;
font-style: normal;
font-weight: bold;
text-align: center;
font-family: Verdana;
font-size: 12pt;
font-style: normal;
font-weight: bold;
border-collapse: collapse;
  font-size: 11px;
<h3>Codeigniter Captcha Helper Example</h3>
<?php echo form_open('captchacontroller/captcha',array('name' => 'captcha', 'method'=> 'post')); ?>
    <table align="center" cellpadding = "5">
            <td>User ID: </td>
            <td><input type="text" size="40px" name="userid" /></td>

            <td>Password: </td>
            <td><input type="text" size="40px" name="password" maxlength="10"/></td>
            <td>Captcha: </td>
            <td><p id="captImg"><?php echo $captchaImg; ?></p><br><input type="text" size="40px" name="captcha" value=""/></td>
            <td colspan="5" align="center">
            <input type="submit" value="Login" name="submit"/></td>
<?php echo form_close();?>

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.

defined('BASEPATH') OR exit('No direct script access allowed');
class CaptchaController extends CI_Controller {
public function __construct()
    public function index() {
    public function captcha() {
$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 */;
echo 'Captcha code did not verified, please try again.';
/* Put your code */;
$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'];
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!