CodeIgniter CAPTCHA Helper

Facebooktwitterredditpinterestlinkedinmailby feather

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.

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

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

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
Facebooktwitterredditpinterestlinkedinmailby feather