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