Adding Captcha to Database using Codeigniter

This tutorial will show you how to generate captcha image using Codeigniter Captcha Helper and store captcha characters into database. The CAPTCHA Helper file contains functions that assist in creating CAPTCHA images.

The CAPTCHA helper

The captcha function requires the GD image library.
Only the img_path and img_url are required.
If a word is not supplied, the function will generate a random ASCII string. You might put together your own word library that you can draw randomly from.
If you do not specify a path to a TRUE TYPE font, the native ugly GD font will be used.
The “captcha” directory must be writable
The expiration (in seconds) signifies how long an image will remain in the captcha folder before it will be deleted. The default is two hours.
word_length defaults to 8, pool defaults to ‘0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ’
font_size defaults to 16, the native GD font has a size limit. Specify a “true type” font for bigger sizes.
The img_id will be set as the “id” of the captcha image.
If any of the colors values is missing, it will be replaced by the default.


Netbeans 8.1
XAMPP in Windows
Codeigniter 3.0.6

Configure XAMPP and Netbeans

From Netbeans IDE go to Tools->Options. Click on PHP. Now on tab “General” browse the file for “PHP 5 Interpreter”. The php interpreter file generally placed inside the <physical drive in Windows OS>:xamppphpphp.exe

Configure Codeigniter and Netbeans

Create a new PHP project in Netbeans. Then remove the index.php file from the newly created project. Now copy the extracted files from Codeigniter 3.0.6 to the newly created project directory.

Directory Structure

A typical directory structure for the project would be as shown below. Here the captcha directory will contain the created captcha image file and the generated captcha characters will be stored into database.

codeigniter captcha directory

Read also Captcha using Codeigniter

Step 1. Now modify <root directory>/application/config/autoload.php file for auto-loading html, url, file, form and session

Step 2. Now modify <root directory>/application/config/config.php file to define some captcha config

Step 3. Now modify <root directory>/application/config/database.php file to add database config

Step 4. Now create captcha table in codeigniter database

Step 5. Create a view file under <root directory>/application/views which will be used as a registeration form with captcha

Step 6. Create a Controller class under <root directory>/application/controllers for handling client’s request and response

Step 5. Now modify <root directory>/application/config/routes.php file for pointing the default controller class

Step 6. Now if everything is fine run the application, you will see below output in the browser. At the same time if you look into database table you will see that captcha characters are stored.

codeigniter captcha

codeigniter captcha

Step 7. Now if you do not put anything and submit the form then error messages displayed

codeigniter captcha

Step 8. If both passwords do not match then you see the below error message

codeigniter captcha

Step 9. If you enter incorrect captcha then you will see below error message

codeigniter captcha

Step 10. If everything is entered correctly then you will see below success message

codeigniter captcha

Thanks for reading.

Leave a Reply

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload CAPTCHA.