Support CakePHP Captcha

CakePHP Captcha component for CakePHP Version 2.x (Demo)

Here is the latest CakePHP Captcha Support for CakePHP Version 2.9+. Tested up to CakePHP version 2.9.3.


Single | Multiple | Math Catcha

Basic Theme

Width: 150, Height: 50, Theme: Basic

Can't read? Reload
Random Theme

Width: 250, Height: 90, Theme: Random

Can't read? Reload
Math Captcha

Answer simple math:

11 - 10 = ?


  • Multiple Captcha Support.
    - It simply supports multiple captchas on a page. In different forms or in a single form.
  • Model Validation attahced as Behavior
  • Image and/or Simple Math Captchas
  • Configurable Model Name, Field Name, Captcha Height, Width, Number of Characters and Font Face, Size, Angle of rotation
  • Works without GD Truetype font support
  • Random or Fixed Captcha Themes for Image Captchaa
  • Random Font face


Place all files bundled in this package in it's corresponding folders.

In Controller

Add in the top definitions of your controller.

var $components = array('Captcha'=>array('Model'=>'Signup', 'field'=>'captcha'));//'Captcha'

Note: "captcha" is the field name for which we are binding this captcha here in examples. Replace with appropriate name.

Add this function in your controller.

function captcha()	{
    $this->autoRender = false;

Add the similar logic to the function which is action of your form, in your controller. The highlighted line is the one which is related to the captcha component.

function add()	{
    if(!empty($this->request->data))	{
        $this->Signup->setCaptcha('captcha', $this->Captcha->getCode('Signup.captcha'));
        if($this->Signup->validates())	{ //as usual data save call
            // validation passed, do save or something
        }	else	{ //or
            $this->Session->setFlash('Data Validation Failure', 'default', array('class' => 'cake-error'));
            //validation not passed, do something else

In Model

Add CaptchaBehaviour in the Model definitions, as following:

public $actsAs = array(
    'Captcha' => array(
        'field' => array('captcha'),
        'error' => 'Incorrect captcha code value'

In View

Add form code in the view file, in the form where you want the captcha image to appear:

echo $this->Form->create("Signups");
echo $this->Form->submit(__(' Submit ',true));
echo $this->Form->end();

And importanly place the following javascript script code in somewhere in your page so it is called properly and execute.

<script src=""></script>
jQuery('.creload').on('click', function() {
    var mySrc = $(this).prev().attr('src');
    var glue = '?';
    if(mySrc.indexOf('?')!=-1)  {
        glue = '&';
    $(this).prev().attr('src', mySrc + glue + new Date().getTime());
    return false;

That should be it!

Few more examples

Example of custom settings:

echo $this->Form->create("Signups");

Example of multiple captchas:

//form 1
echo $this->Form->create("Signups");

//form 2, A math captcha, anywhere on the page
echo $this->Form->create("Users");

Options which can be set in your view file with form, are:

  • model: model name.
  • field: field name.
  • type: image or math. If set to 'math' all settings given below are ignored
  • width: width of image captcha
  • height: height of image captcha
  • theme: theme/difficulty image captcha
  • length: number of characters in image captcha
  • angle: angle of rotation for characters in image captcha

There are a few other options which, at the moment, can be set in the component file directly


Download at Github

Change Log (Updated on) – Septermber 10, 2014

  • Model Validation works as Behavior
  • Supports Multiple Image and Simple Math Captchas
  • Works without GD Truetype font support (NOT RECOMMENDED though)
  • Default and Random themes for Image Captcha
  • Checks for missing font file

If you should have any question or comment regarding the use of this captcha component please post it here