×

CodeIgniter CAPTCHA Helper

A CAPTCHA (Complete Automated Public Turning test to tell Computer and Humans Apart) helper is a function that is used in web application to check whether the user is human or machine. It provides a randomly generated string to identify the user. In this way, it protects the web application from various spams.

A CAPTCHA is an important part of a web application, where the action is processed based on the user’s inputs or responses.

A CAPTCHA helper file has some predefined function that is used to create CAPTCHA images.

  • Loading the helper

It is used to load the helper class and passes the helper function in the controller’s method.

Syntax: - $this->load->helper(‘captcha’);

Configuration of CAPTCHA helper

  • img_path: It defines the path where all the CAPTCHA images are stored.
  • img_url: It defines the URL, where the CAPTCHA images are shown.
  • img_width: It represents the width of the CAPTCHA image. By default, it is 150.
  • img_height: It represents the height of the CAPTCHA image. By default, it is 30.
  • word_length: It is optional. You can define it or leave it. By default, it is 8.
  • font_size: It is also optional. It defines the size of the CAPTCHA word. The default font size is 16.
  • font_path (optional): It defines the path where the font directory is stored.
  • expiration (in seconds): It shows that for how long the picture can be stored in the CAPTCHA folder before it gets deleted. By default, it remains in the folder for 2 hours.
  • pool: It is an optional feature. It is used in the CAPTCHA to specify the type of letters you want to use. By default, it uses ‘0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ’;

Example of Predefined data in CAPTCHA Helper:

Before loading the CAPTCHA helper in Controller, you need to create a folder in CodeIgniter application where you can save your CAPTCHA images.

In the following image, we have created a folder named captcha_images.

CodeIgniter CAPTCHA Helper

Example: Create a Tests.php file in application/controller folder and write the following program:

Tests.php

<?php
 class Tests extends CI_Controller {
  public function index()
 {
      echo "<title> Welcome To Tutorials And Examples </title>"; 
     $this->load->helper('CAPTCHA'); // load the captcha helper
     $info = array ( 
      'word' => "AMIT YADAV",  // Predefined names
         'img_path'      => 'captcha_images/',
         'img_url'       => base_url().'captcha_images/', 
         'font_path'  => 'system/fonts/texb.ttf',
         'img_width'     => '250',
         'img_height'    => '70',
         'word_length'   => '10',
         'font_size'     => '20',
         'colors'        => array (          // White background and border, black text and red grid
           'background' => array (100, 125, 255),
           'border' => array (255, 100, 255), 
           'text' => array (2, 7, 0),
           'grid' => array (125, 40, 40)
   )     
 ); 
 echo "<h2>Captcha Images: Predefined Data </h2>"; 
 $captcha = create_captcha($info);
 echo $cap['image'];
 }
 } 
 ?> 

Let’s execute the above program by invoking the URL localhost/CodeIgniter-3.1.11/index.php/test/. It shows the output, as shown below.

CodeIgniter CAPTCHA Helper

Example of Random generated images in CAPTCHA Helper:

As the name suggests, random CAPTCHA images are generated in this web application. Now create a Tests.php file in application/controller folder and write the following program:

Tests.php

<?php
 class Tests extends CI_Controller {
  public function index(){
      echo "<title> Welcome To Tutorials And Examples </title>"; 
     $this->load->helper('CAPTCHA');
     $vals = array (
         'img_path'      => 'CAPTCHA_images/',
         'img_url'       => base_url().'CAPTCHA_images/', 
         'font_path'  => 'system/fonts/texb.ttf',
         'img_width'     => '250',
         'img_height'    => '70',
         'word_length'   => '10',
         'font_size'     => '20', 
         'pool'          => '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ',
         'colors'        => array (     // it is used to change the background color, border, text
           'background' => array (100, 125, 255),
           'border' => array (255, 100, 255),
           'text' => array (2, 7, 0), 
           'grid' => array (125, 40, 40)
   )
 ); 
 echo "<h2>Captcha Images: Predefined Data </h2>"; 
 $CAPTCHA = create_CAPTCHA($info);
 echo $cap['image'];
 }
 }
 ?> 

Let’s execute the above program by invoking the URL localhost/CodeIgniter-3.1.11/index.php/test/. It shows the output, as shown below.

When we refresh the page, it produces a new CAPTCHA image, as shown in the following image.

CodeIgniter CAPTCHA Helper

Related Topics

CodeIgniter Views

A View is a presentational part which shows the information to the users. It is a complex web page which contains navbar, header, footer, sidebar, etc. A view file cannot be called directly...

5 minutes read.

CodeIgniter User Agent Class

User Agent Class The CodeIgniter provides a user agent class that helps to identify and collect information about the browser, mobile devices, or robots visiting your site. Besides this, it is also used...

4 minutes read.

CodeIgniter Benchmarking Class

The CodeIgniter contains the Benchmarking class that is used to calculate the time difference between two points or the memory usage of the passed statements in the application. By default, the Benchmarking...

7 minutes read.

Delete a record from Database CodeIgniter

Delete a record from the Database After inserting, retrieving, and updating table records, we will now learn how we can delete a particular record from a database table. To delete a record, first, we...

6 minutes read.

CodeIgniter ZIP Encoding Class

ZIP Encoding Class The CodeIgniter provides a Zip encoding class that are used to compress a large file into a Zip archives. And this archives files can be downloaded either to your desktop or...

5 minutes read.

HTML Table Class CodeIgniter

HTML Table Class The CodeIgniter framework provides an HTML Table Class that is used to develop an auto-generated table using a defined array or result sets of the database table. Loading an HTML Table Class...

8 minutes read.

CodeIgniter Pagination Class

Pagination Class The CodeIgniter framework provides a pagination class that is used in the data list to create pagination links in a web application. Generally, it is used to retrieve data records from...

10 minutes read.

Image Manipulation Codeigniter

Image Manipulation A Codeigniter provides an image manipulation class that is used for resizing, cropping, rotation, and many other manipulation tasks. This class also support three major image libraries such as GD/GD2, NetPBM, and...

11 minutes read.

CodeIgniter Controller

As its name suggest that it is a Controller that controls the web application, and it works as an intermediary between the Models and the View. A controller takes requests from the user...

3 minutes read.

CodeIgniter String Helper

The string helper file contains functions that allows separate operations with strings in CodeIgniter. Loading the String Helper Before using the string helper in the CodeIgniter application, you must load it in the controller...

7 minutes read.

CodeIgniter Cookie Helper

A cookie is a small set of files sent from the web server to the end-user system. In Helpers, the cookie helper file has some predefined functions that are used to the...

4 minutes read.

CodeIgniter Tutorial

What is CodeIgniter? CodeIgniter is an open-source framework for developing a dynamic web application with the use of PHP. It uses the MVC framework to develop the project, which is much...

3 minutes read.

CodeIgniter Date helper

A date helper file contains some predefined functions that work with date functions. Before using the date function in CodeIgniter, you have to load the date helper in the controller. For example: $this->load->helper(‘date’); Functions of...

12 minutes read.

CodeIgniter Libraries

The library is an important part of the CodeIgniter framework. It has a large collection of libraries files that are used to increase the performance of an application. By default, all CodeIgniter libraries...

4 minutes read.

CodeIgniter Language Class

Language Class The CodeIgniter’s language class contains the various functionality that is used to access the language-specific files and text line for the purposes of internationalization. This class provides you suitability to add or...

4 minutes read.

CodeIgniter Helpers

The Helpers (functions) are stored in the Helper file. It helps CodeIgniter to perform different tasks. The helper file is the collection of many functions that is used to perform specific task. It...

3 minutes read.

CodeIgniter Array Helper

The Array Helper contains some predefined functions that are used to perform the various operation with array. Load Array helper It is used to load the helper class. We can pass the helper function in the...

6 minutes read.

Database Configuration CodeIgniter

In every application, there is an important part of a database through which developers can connect an application to store users’ data and perform various functions in the database application such as inserting,...

10 minutes read.

CodeIgniter Number helper

The number helper file contains some predefined function that deals with numeric data to display numbers in bytes format. Load a Number Helper You must load a number helper file in the controller to perform...

3 minutes read.

CodeIgniter Email Class

CodeIgniter provides an e-mail library that helps to send a message from one application to another. You can create and send text messages easily in CodeIgniter application even you can set email...

9 minutes read.