Simple captcha component for CakePHP (New)

Referencing to an earlier post on CakePHP Captcha Component i.e. http://www.devarticles.in/cakephp/simple-captcha-component-for-cakephp which i had posted quite a few time ago. Recently i got chance to modify an available phpcaptcha script to suit the cakephp and it turned out to be more precised bit of code and not to say, very simple to install. Additionally it would not require any temp folder for images and fonts as in the last component script. (Download files Here)
Please note that this new one is not an upgraded version of earlier one, it is completely a different piece of code.

  • Copy the attached font file (monofont) to ‘webroot’ folder.
  • Copy the component file (captcha.php) to app/controllers/components
  • Create a function similar to the following in your controller

    function captcha() {
    //$width = 120;
    //$height = 40;
    //$characters = 6;

    /*Lines above indicate how image width, height and number of characters
    on captcha image could be set, if set just pass them to create() method in
    the same sequence respectivaly. For example example :
    $this->Captcha->create($width, $height, $characters)*/

    App::import('Component','Captcha');
    $this->Captcha = new CaptchaComponent($this);
    $this->Captcha->create();
    }

  • Call the captcha action from within your img tag in the view file as below:

    < ?php echo $html->image('users/captcha',
    array('style'=>'border:1px #ccc solid','vspace'=>2)
    );
    ?>

And you are done! The captcha image should look like
Also, you can re-generate the captcha by adding ajax code something similar to this:

< ?php echo $html->image('users/captcha', array('style'=>'border:1px #ccc solid','vspace'=>2)); ?>
< ?php echo $ajax->link('Can not read this code?','re-generate captcha',array('url'=>'users/captcha','update'=>'captchaID','loading'=>'do this','loaded'=>'do that')); ?>

Now you should have a Cakephp session variable named “security_code” set. This variable is set in CaptchaComponent and can be access via $this->Session->read(’security_code’) in your controller action. Compare it with the input field data submitted and you are done. Let me know if you have any problem executing this.
Download files