Captcha-Component-for-CakePHP-2.x

Simple CakePHP captcha component for CakePHP 2.x

Captcha-Component-for-CakePHP-2.x

Here is the latest CakePHP Captcha component for CakePHP Version 2.x. Compatible up to CakePHP version 2.5.3.

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 at Github

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.

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();
 }

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

echo $this->Session->flash();
echo $this->Form->create("Signups");
$this->Captcha->render($captchaSettings);
echo $this->Form->submit(__(' Submit ',true));
echo $this->Form->end();

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 at Github
Download Captcha Component for Cakephp 1.x

26 thoughts on “Simple CakePHP captcha component for CakePHP 2.x

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

  2. 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.

  3. 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.

  4. 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

  5. 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.

  6. 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.

  7. 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?

    1. It seems that your code is sending an output to browser before doing the HEADER call. (Check http://ow.ly/prO0G know know what does it mean). Even a space can cause this. Please check your code and log files in order to fix this.

  8. 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.

  9. 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

  10. 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.

  11. 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.

  12. 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?

  13. 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

  14. 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 :(

  15. It works fine. Please download the latest version again and try. There was a space at the beginning of component file which caused the “Headers already sent..” issue to not generate the image captcha. It is fixed now. Thanks. Arvind.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>