×

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 URL Helper

The helper file contains some predefined functions that are used to work with URL, such as generate the current file path, transfer control, redirect, create a link, etc. Load the URL Helper Before using...

7 minutes read.

CodeIgniter Text Helper

The text helper contains the various function that is used to operate with text such as define a word limit and characters limit, conversion of word from ASCII to entities, etc. Loading the text...

4 minutes read.

CodeIgniter Calendaring Library

The calendar library contains various functions that are used to create a dynamic calendar. It also enables you to pass the data to calendar cells and 100% control over the calendar design. Load...

7 minutes read.

Download Helper Codeigniter

Download Helper Codeigniter: The download helper function is used to download data from the server to your computer. The data can be any format like text, jpg, mp3, mp4, etc. Loading...

2 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 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 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 URI Class

URI Class The CodeIgniter contains a URI class that is used to retrieve information from URI strings. It also provides suitability to get information from the re-routed segments. The system automatically initializes this...

6 minutes read.

Codeigniter Session Library

Session Library The session is an essential part of any application. It contains various functions to manage the users' status and track their activity while browsing on the site. For example,...

13 minutes read.

Structure of Codeigniter

The File structure of CodeIgniter is divided into three parts: ApplicationSystemuser_guide Application -: As the name represents Application, which contains all the main parts of the project like a controller, libraries, view, models, and others....

3 minutes read.

Versions of CodeIgniter

CodeIgniter v3.1.11 (This is the current version which we will used in this tutorial)CodeIgniter v3.1.10CodeIgniter v3.1.9CodeIgniter v3.1.8CodeIgniter v3.1.7CodeIgniter v3.1.6CodeIgniter v3.1.5CodeIgniter v3.1.4CodeIgniter v3.1.3CodeIgniter v3.1.2CodeIgniter v3.1.1CodeIgniter v3.1.0CodeIgniter v3.0.6CodeIgniter...

1 minute read.

Installation of CodeIgniter

Before installing CodeIgniter, you need to make sure that the wamp or xampp server in the system is preinstalled. The installation procedure for CodeIgniter is very easy. You can download CodeIgniter by following these...

2 minutes read.

CodeIgniter Security Class

Security Class The security class provides various function that helps to create a secure application and process input data securely to the application. This class is preloaded in the CodeIgniter application, so you...

4 minutes read.

Form Validation Library Codeigniter

Form Validation Library The Codeigniter provides a form validation library that contains predefined rules to validate the form fields. It also helps in optimizing the form validation code, which reduces the effort and time...

31 minutes read.

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...

3 minutes read.

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 File Helper

The file helper function is used to perform various tasks with files such as accessing files, writing data to files, deleting files, and more. Loading the File Helper Before using the file helper, you must...

7 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 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.

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.