In this blog we will explain how to create captcha in CodeIgniter using captcha helper.
What is captcha ?
Captcha is randomly generate string and in CodeIgniter using captcha we can verify whether the user is human or not.
How many function require in create CAPTCHA in CodeIgniter?
- GD image library
- The “captcha” directory must be writable
- word_length defaults to 8,
- ont_size defaults to 16
How to create CAPTCHA using captcha helper?
In codeigniter provides a CAPTCHA helper to create a random code and create captcha image. CAPTCHA helper contains functions so can create capcha images with various format.
Below , we will provide some example to create captcha functions in CodeIgniter.
1- Create and display captcha image
2- Check user input and submit for compare
3- compare both value ,user input and captcha value
Create and display capcha image in codeigniter:
To create captcha image , you need to specify the config options and pass value in array and use create_captcha() in captcha helper.
// Captcha configuration
$config = array(
‘img_path’ => ‘captcha_images_folder/’,
‘img_url’ => base_url().’captcha_images_folder/’,
‘img_width’ => ’50’,
‘img_height’ => 50,
‘word_length’ => 8,
‘font_size’ => 16
);
$captcha = create_captcha($config);
Implement CAPTCHA Functionality in CodeIgniter :
Now we will show you can use capycha helper you can create CAPTCHA code in CodeIgniter application.
Make Controller (Captcha.php)
The Captcha controller contains 3 functions, __construct(), index(), and refresh().
__construct()
Load the CodeIgniter CAPTCHA helper to generate captcha code and image.
Load the Session library to store captcha code for comparison.
index()
Generate random word and create captcha image using create_captcha() function.
Store captcha code in a SESSION variable.
Pass captcha image to view and load the view.
Handles the captcha code submission and comparison process.
refresh()
Generate random word, create and display captcha image. Basically, this function is called when the user request for a new captcha.
<?php defined(‘BASEPATH’) OR exit(‘No direct script access allowed’);
class Captcha extends CI_Controller
{
function __construct() {
parent::__construct();
// Load session library
$this->load->library(‘session’);
// Load the captcha helper
$this->load->helper(‘captcha’);
}
public function index(){
// If captcha form is submitted
if($this->input->post(‘submit’)){
$inputCaptcha = $this->input->post(‘captcha’);
$sessCaptcha = $this->session->userdata(‘captchaCode’);
if($inputCaptcha === $sessCaptcha){
echo ‘Captcha code matched.’;
}else{
echo ‘Captcha code does not match, please try again.’;
}
}
// Captcha configuration
$config = array(
‘img_path’ => ‘captcha_images/’,
‘img_url’ => base_url().’captcha_images/’,
‘font_path’ => ‘system/fonts/texb.ttf’,
‘img_width’ => ‘160’,
‘img_height’ => 50,
‘word_length’ => 8,
‘font_size’ => 18
);
$captcha = create_captcha($config);
// Unset previous captcha and set new captcha word
$this->session->unset_userdata(‘captchaCode’);
$this->session->set_userdata(‘captchaCode’, $captcha[‘word’]);
// Pass captcha image to view
$data[‘captchaImg’] = $captcha[‘image’];
// Load the view
$this->load->view(‘captcha/index’, $data);
}
public function refresh(){
// Captcha configuration
$config = array(
‘img_path’ => ‘captcha_images/’,
‘img_url’ => base_url().’captcha_images/’,
‘font_path’ => ‘system/fonts/texb.ttf’,
‘img_width’ => ‘160’,
‘img_height’ => 50,
‘word_length’ => 8,
‘font_size’ => 18
);
$captcha = create_captcha($config);
// Unset previous captcha and set new captcha word
$this->session->unset_userdata(‘captchaCode’);
$this->session->set_userdata(‘captchaCode’,$captcha[‘word’]);
// Display captcha image
echo $captcha[‘image’];
}
}
View (captcha/index.php)
The captcha image is shown with an input field and submit button. Once the user submits the captcha word it will be sent to the index() method of Captcha controller for comparison.
To Submit Captcha Code
Can’t read the image? click here to refresh. Enter the code :
Also, the user can request a new captcha image by refresh link. The jQuery and Ajax code will execute by clicking the refresh link. The new captcha image will be fetched from the refresh() method of Captcha controller and the existing captcha image will be replaced with a new image.
CAPTCHA Configuration Options
Many configuration options are available in create_captcha() function to customize the captcha image. But few function useful configuration options are given below:
img_path – Required. The path where the captcha images will be stored.
img_url – Required. The URL of the captcha image.
img_width – Optional. The width of the captcha image. Defaults to 150.
img_height – Optional. The height of the captcha image. Defaults to 30.
word_length – Optional. The number of characters. Defaults to 8.
font_size – Optional. The font size of the captcha word. Defaults to 16.
font_path – Optional. The path of text font. Specify the font path, if you want to use different text font for captcha word.
pool – Optional. Defaults to ‘0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ’. Specify the letters which you want to use in the captcha code.