CakePHP captcha support for CakePHP 2.x and 3.x

Here is the latest CakePHP Captcha support for CakePHP Version upto 3.x.

Download for CakePHP 3.x
Download for CakePHP 2.x

NOTE:For best Image Captcha results this component requires GD and Freetype support enabled. Please check your phpinfo() to know whether your server supports this (See image below). Contact your hosting provider if any of these libraries is not enabled.

gd-free-type-enabled

Captcha-Component-for-CakePHP-2.x

Online Demo
Download 3.x at Github
Download 2.x at Github

Update – Jan, 2018

  • Tested with CakePHP 3.5x

Update – April 23, 2017

  • Google Recaptcha Support included

Update – Aug 09, 2016

  • CakePHP Captcha Support bundled as a plugin

Update – Oct 30, 2015

  • CakePHP 3.x support

Update – Sep 05, 2014

  • Fixed ‘headers already sent’ issue
  • Changed to render image captcha by Default in example
  • Updated documentation

Update – Sep 25, 2013

  • Supports Image and Simple Math captcha
  • Works without GD Truetype font support (NOT RECOMMENDED though)
  • Default and Random themes for Image Captcha
  • Checks for missing font file

Updated on – Sep 19, 2013

  • Had been reported of issues related to missing font file so i have updated the controller function to detect the existence of font file. Dies with error on missing font file. So don’t forget to upload attached font file. If you wanted to use a different font upload it to webroot folder and change settings[‘font’] parameter

Updated on – April 12, 2013

  • Random captcha images are possible. Set “theme”=>”random” in $settings variable of CaptchaComponent.php or in Controller when loading captcha component.
  • Can’t Read? Reload is possible now. A working piece of jQuery code is included in view file add.ctp. Be sure to include jquery library, such as https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js to make Reloading of captcha working

How to make it working

Download attached zip file and extract. Copy all files placed in app folder to their corresponding locations.

I created “SignupsController” controller and Signup.php model for demonstration purpose. Create a function similar to the following in your controller.

[php]
function captcha() {
$this->autoRender = false;
$this->layout=’ajax’;
if(!isset($this->Captcha)) { //if Component was not loaded through $components array()
$this->Captcha = $this->Components->load(‘Captcha’, array(
‘width’ => 150,
‘height’ => 50,
‘theme’ => ‘random’, //possible values : default, random ; No value means ‘default’
)); //load it
}
$this->Captcha->create();
}
[/php]

Call the captcha action from within your form tag in the view file, as below:

[php]
echo $this->Session->flash();
echo $this->Form->create("Signups");
$this->Captcha->render($captchaSettings);
echo $this->Form->submit(__(‘ Submit ‘,true));
echo $this->Form->end();
[/php]

And you are done! The captcha image should look like Cakephp Captcha COmponent

This component includes a model file i.e. Model/Signup.php file to demonstrate the use of model validation for captcha input by user. The validation works just like any other custom model validation in CakePHP.

Download 3.x at Github
Download 2.x at Github

Download Cakephp 1.x

39 thoughts on “CakePHP captcha support for CakePHP 2.x and 3.x

  1. I still get spam on my form even if the captcha is used. How come? is there a bug or exploit in it? I even checked to see what the bot entered as the captcha code as is identical as the challange captcha.. help?

  2. Hello,

    I am using Captcha in cakephp 3 form the ref link “https://github.com/inimist/cakephp-captcha-3”.

    But image is not comes. Please help me. I have placed properly the code and files in respective files and folders. Still image not comes.

    Thanks.

  3. Hi,

    How can I use this library for automation script. For test automation. Of is there any other module available for this.

    Please let me know. Thanks

  4. Strict (2048): Declaration of CaptchaBehavior::beforeValidate() should be compatible with ModelBehavior::beforeValidate(Model $model, $options = Array) [APP\Model\Behavior\CaptchaBehavior.php, line 27

    also image is not showing… :( help

  5. excellent component..
    thanks for post

    I have this error
    Strict (2048): Declaration of CaptchaBehavior::beforeValidate() should be compatible with ModelBehavior::beforeValidate(Model $model, $options = Array) [APP/Model/Behavior/CaptchaBehavior.php, line 26]

    I do not understand what is happening

      1. fix it
        line change in behavior
        public function beforeValidate (Model $ model) {
        about
        public function beforeValidate (Model $ model, $ options = array ()) {

  6. Hi, 1st of all, sorry for bad english, i’m french .

    I have a problem when i ‘m trying to implement your captcha component : i get an error Missing behavior :
    Error: CaptchaBehavior could not be found.
    Error: Create the class CaptchaBehavior below in file: app\Model\Behavior\CaptchaBehavior.php

    I suspect the “public $actsAs” in my model, but i’m unable to find where is the problem.

    Could you help me debugging ?

    thanx

  7. Sorry, i got the solution. There was a space before the open php tag at the begining of CaptchaComponent.php.

  8. Sorry, i’m not still getting the error “header already sent” , but the image doesn’t appears. The link to the image returns a code that starts with “ÿØÿàJFIFÿþ>CREATOR: gd-jpeg v1.0 (using IJG JPEG v80), default quality” and follows with an alpha numeric code. But no image. Can you help me? Thanks.

  9. Hello,

    i have the same problem reported by other people, i’m getting a “Cannot modify header information” error. I downloaded a new version of Cakephp, installed the component (without any other controller or anything else) and still getting the error. Any ideia?

    Thanks,
    Henrique.

  10. 1: Thanks Arvind…thanks!
    2: Sorry for my bad English!
    3: Just to share my experience:
    If you are using Cakephp 2.4.X and captcha image is not displayed on your remote web hosting server, I solved it by removing @ operator from
    $image =@ imagecreate($width, $height) or die(‘Cannot initialize new GD image stream’);
    and change it to
    $image = imagecreate($width, $height) or die(‘Cannot initialize new GD image
    in file CaptchaComponent.php
    and i now using this GREAT component without any problem!
    Hope this help someone

  11. Good afternoon everyone. I’m using version 2.4.3 on hostgator and captcha image does not work but the maths works perfectly. Another thing: in an action to add, even with the right answer the captcha, I’m being redirected to a blank page. Can anyone help me. There’s nothing in the Apache logs.

  12. Hi Arvind,
    Your CAPTCHA component helps me a lot. We have used math CAPTCHA in our site. We experienced a bug in it. That is, it allows the user to submit the form when the answer is double quote (“). So, I have updated the validation script (in my model) by allowing only numeric values for CAPTCHA field. Please resolve this bug and it will be useful for all users who all are going to use your stuff. Thanks!

    1. I guess you are wanting the user only entering a numeric value in case of math captcha? Actually i cannot say it a ‘bug’. The reason is that, the captcha validation requires the ‘answer’ to be CORRECT so unless user enters the CORRECT answer it wont let pass the form as the validation simply would fail. Or do you say that user can pass the validation by entering a double quote? I don’t see such situation anyways.

      In order to let user enter only numeric you can consider adding a javascript code.

  13. Interestingly, it is working on my local machine but not on my server. Both GD support and FreeType Support enabled on the server. I just see a broken image. When I open the image link on a new tab this is what I get Warning (2): Cannot modify header information - headers already sent by (output started at /home4/user/public_html/app/Controller/Component/CaptchaComponent.php:1) [APP/Controller/Component/CaptchaComponent.php, line 173] and the rest is mumbo jumbo code. Any idea why?

  14. i got the solution
    by
    // $textbox = imagettfbbox($font_size, 0, $this->settings[‘font’], $code)
    $textbox = imagestring($image, 300, 15, 5, $code, $text_color)
    or die(‘Error in imagettfbbox function’);
    $x = ($width – $textbox[4])/2;
    $y = ($height – $textbox[5])/2;
    $y -= 5;
    imagestring($image, 300, 15, 5, $code, $text_color) or die(‘Error in imagettftext function’);

    1. It works with imagestring but you may not control the font size. It you want to use this component to show image captcha, using it while “GD enable with Freetype” is recommended.

      *Update*
      You can show a simple math question as well. Set captchaType=math in settings.

  15. sir,
    the problem is with linux server
    when i run the captcha folder seperately at server
    it produces this error
    Fatal error: Call to undefined function imagettfbbox() in /home/idiamond/public_html/mindspirit/app/Controller/Component/CaptchaComponent.php on line 69

  16. dear, ur code helped me a lot, i succeeded locally, but when i uploaded this to server, the image is not coming, i tried to give various path but failed, kindly help me out to sort this.

  17. Hi. I have the same problem as Rikita. When i echo debug($code_captach) see a code the image like : . But in normal HTML code image not show. On localhost works fine but on server isn’t show.

  18. The image reload is not ajax? When i click on it it just goes to the top of the page. It would be nice when you click on the image the image changes without laving the page. Can this be done using javascript.onclick?

  19. I also faced the issue. And I am also using CakePHP2.3.5. I found the problem was with the line:

    imagettfbbox($font_size, 0, $this->settings[‘font’], $code) or die(‘Error in imagettfbbox function’);

    The imagettfbox was giving problem.
    In my case when I was testing on my pc (windows) then localhost://test/captcha was rendering the image properly. But same code was not rendering when I uploaded it to my hosting service (linux). I guess it could have been issue that imagettfbbox could not find the font (though it is present in webroot).
    So, I replaced the line with:
    imagestring($image, 300, 15, 5, $code, $text_color);
    and my captcha working fine now.

    Rest is working fine. Would like to thanks Arvind for useful work.

    1. hey,i m having the same issue and found this at linux server
      Fatal error: Call to undefined function imagettfbbox() in /home/idiamond/public_html/mindspirit/app/Controller/Component/CaptchaComponent.php on line 69

      so tell me that where u placed
      imagestring($image, 300, 15, 5, $code, $text_color);
      in your code

  20. Image is not showing?
    I loaded the jquery end of page then JS code
    Captcha component is imported
    I am using cakephp 2.3.5
    Any advise :(

Leave a Reply