Captcha library for CodeIgniter

First you need to save this image in some directory.

I have placed it in system/application/themes folder

hi here is the library for captcha:

<?php if (!defined(‘BASEPATH’)) exit(‘No direct script access allowed’);
* this class is created by: mohammad tareq alam
* email:
* blog:
class Captcha {

var $CI = null;

function Captcha()
$this->CI =& get_instance();

function captchaImg()

$RandomStr = md5(microtime());// md5 to generate the random string

$ResultStr = substr($RandomStr,0,5);//trim 5 digit

$NewImage =imagecreatefromjpeg(APPPATH.”themes/img.jpg”);//image create by existing image and as back ground

$LineColor = imagecolorallocate($NewImage,233,239,239);//line color
$TextColor = imagecolorallocate($NewImage, 255, 255, 255);//text color-white

imageline($NewImage,1,1,40,40,$LineColor);//create line 1 on image
imageline($NewImage,1,100,60,0,$LineColor);//create line 2 on image

imagestring($NewImage, 5, 20, 10, $ResultStr, $TextColor);// Draw a random string horizontally

$newdata = array(
‘captchaKey’ =>$ResultStr


//$_SESSION[‘key’] = $ResultStr;// carry the data through session

header(“Content-type: image/jpeg”);// out out the image

imagejpeg($NewImage);//Output image to browser


function captchaText()

$RandomStr = md5(microtime());// md5 to generate the random string

$ResultStr = substr($RandomStr,0,5);//trim 5 digit

$newdata = array(
‘captchaKey’ =>$ResultStr


return $ResultStr;


function validateCaptcha()

$key = $this->CI->session->userdata(‘captchaKey’);
$number = $_REQUEST[‘number’];
$msg = ‘<center><font face=”Verdana, Arial, Helvetica, sans-serif” color=”#FF0000″>
String not valid! Please try again!</font></center>’;
return $msg;
else{ return ‘success’;}


function in controller:

function captchaImg()
$captchaImg = $this->captcha->captchaImg();
return $captchaImg;


in view:


<img src=”<?php echo base_url()?>index.php/controllername/captchaImg”/>

<input name=”number” type=”text” id=\&quot;number\&quot;>

<input type=”submit”  value=”check”>


In controller again for validating captcha:

$validateCaptcha = $this->captcha->validateCaptcha();
$captchaText = $this->captcha->captchaText();

//do something


Thats it. hope you can use it. Its very simple



Work is fun!

Tagged with: , , , , ,
Posted in PHP, PHP Frameworks, Tips And Tricks

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

All content provided on this "" blog is for informational purposes only. The owner of this blog makes no representations as to the accuracy or completeness of any information on this site or found by following any link on this site. The owner of [] will not be liable for any errors or omissions in this information nor for the availability of this information. The owner will not be liable for any losses, injuries, or damages from the display or use of this information.
August 2010
« Jul   Nov »

Enter your email address to follow this blog and receive notifications of new posts by email.

Join 23 other followers

%d bloggers like this: