CakePHP Plugin to show Google Recpatcha, Image and Math Challenge, CakePHP 1, 2 & 3
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.

Cakephp Captcha 1 & 2

CakePHP 3 Version is here


A CakePHP Captchas support with automatic Model Validation. Tested upto CakePHP 2.9.3.


  • 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 corresponding folders. Then follow instructions given below.

Configuration (optional)

Open app/Plugin/Captcha/Controller/Component/CaptchaComponent.php file and make necessary changes in the $settings variable defined near line 125.


Follow instructions given below to place code in Controller, Model and View files.

###In Controller

Add in the top definitions, in $uses, $components and $helpers in your controller, as per the CakePHP convensions.

var $components = array('Captcha.Captcha'=>array('Model'=>'Signup', 

var $uses = array('CurrentModel'); //replace "CurrentModel" with your current. The default model in this example is "Signup"

public $helpers = array('Captcha.Captcha'); // and the default helpers like Form, Session, HTML etc

//(Thanks to "Vasile Martiniuc" for pointing out the error due to different model than Signup in $uses :))

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 the "action" of your form, in your controller. The highlighted line (line 3 here) is the one which is related to the captcha component.

function add()	{
    if(!empty($this->request->data))	{
        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' => 
            //validation not passed, do something else

###In Model

Add CaptchaBehaviour in the Model definitions, as following:

public $actsAs = array(
    'Captcha.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.

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!

##More examples

###Custom settings:

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

###Multiple captchas:

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

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

Settings that can be set in your view file:

  • model: model name.
  • field: field name.
  • type: image or math. If set to 'math' all following settings will be obsolete
  • 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

Additional settings that can be set in Component file.

  • fontAdjustment: Responsible for the font size relational to Captcha Image Size
  • reload_txt: The phrase which appears as a Captcha Reload link
  • clabel: Label for Image Captcha Value input field
  • mlabel: Label for Math Captcha Value input field

What's New

  • Tested upto CakePHP 2.9.3
  • Multiple Image and Simple Math captcha
  • Default and Random themes for Image Captcha
  • Multiple font files placed in Lib
  • Fully controlled from View file through Helper



Licensed under The MIT License Redistributions of files must retain the given copyright notice.


Copyright (C) Arvind Kumar,

Looking for a CakePHP Developer?