How To Create Captcha With Codeigniter

Leave a Comment
In this tutorial I will show you how to use the the codeigniter captcha helper.

The CAPTCHA Helper file contains functions that assist in creating CAPTCHA images.

Frist we have load library of captcha in controller.

$this->load->helper('captcha');
Now we will create controller for the CAPTCHA so let's start.Before we create controller we will create captcha folder in root directory of project.



<?php
/**
* 
*/
class Captcha extends CI_Controller
{
 
 function __construct()
 {
  parent::__construct();
  $this->load->helper('captcha');
  $this->load->helper('url');
 }

 function index()
 {
  $vals = array(
      'img_path'  => './captcha/',
     'img_url'  => base_url().'/captcha/',
     'img_width' => 150,
     'img_height' => '50',
     'expiration' => 7200
     );

 $cap = create_captcha($vals);
 print_r($cap['word']);
 echo $cap['image'];
 }
}

?>
EXPLANATION : 1) In construct method of the controller we load captcha library for the captcha and url library for the base_url()
2) In Index method we create array of captcha.
3) Only the img_path and img_url are required.Other are optional.
4) The "captcha" folder must be writable (666, or 777).
5) 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.
So that's it we create captcha for us.Our captcha look like this.



print_r($cap['word']); will print the word which is in captcha image we can use this word for the checking user is enter wrong value or right.

echo $cap['image']; will for the image which is shown as captcha.
If we look in captcha folder the image is store in that folder.



Helpful links


0 comments:

Post a Comment

Powered by Blogger.