Simple Way to Create Captcha in CakePHP 1.2,CakePHP 1.3, CakePHP 2.0 and CakePHP 2.1

Published on : April 13, 2012

Author:

Category: Our Blog


CakePHP 2.1 :Put the “captcha” folder on path-to-cakephp-2.0/vendors/ – on root vendors folder. Use following


App::import('Vendor', 'captcha/captcha');

CakePHP 2.0 :Put the “captcha” folder on path-to-cakephp-2.0/app/vendors/ – on App’s vendors folder. Use following


App::import('Vendor', 'captcha/captcha');

CakePHP 1.3 :Put the “captcha” folder on path-to-cakephp-1.3/vendors/ – on the root vendors folder.


App::import('Vendor', 'captcha/captcha');

CakePHP 1.2 :This post and code is for CakePHP 1.2 and CakePHP 1.3. For CakePHP 1.3 put the “captcha” folder on path-to-cakephp-1.3/vendors/ – on the root vendors folder.

When i start developing site using CakePHP, I was at wits end about how to implement capcha. I found couple ways using google but all of them seemed hard to implement. Now i am going to show you can do it in an easy simple way.There are many ways to add captcha to your CakePHP site. I am going to show a easy way to do it. First Lets create a folder named “captcha” in vendor folder. Lets develop the vendor code.


class captcha {
    public function show_captcha() {
        if (session_id() == "") {
            session_name("CAKEPHP");
            session_start();
        }

        $path= VENDORS.'captcha';
        $imgname = 'noise.jpg';
        $imgpath  = $path.'/images/'.$imgname;

        $captchatext = md5(time());
        $captchatext = substr($captchatext, 0, 5);
        $_SESSION['captcha']=$captchatext;

        if (file_exists($imgpath) ){
            $im = imagecreatefromjpeg($imgpath);
            $grey = imagecolorallocate($im, 128, 128, 128);
            $font = $path.'/fonts/'.'BIRTH_OF_A_HERO.ttf';

            imagettftext($im, 20, 0, 10, 25, $grey, $font, $captchatext) ;

            header('Content-Type: image/jpeg');
            header("Cache-control: private, no-cache");
            header ("Last-Modified: " . gmdate ("D, d M Y H:i:s") . " GMT");
            header("Pragma: no-cache");
            imagejpeg($im);

            imagedestroy($im);
            ob_flush();
            flush();
        }
        else{
            echo 'captcha error';
            exit;
        }
    }
}

This code actually very simple. First i check for session. Then i check for a noise image. Then i create the image. I added the cache control here for any caching problem.

Now we implement it on our site. to do that we have to add a function the controller or we can add it to our pages controller.


    function captcha_image(){
        App::import('Vendor', 'captcha/captcha');
        $captcha = new captcha();
        $captcha->show_captcha();
    }

Then we call it in our view.


</pre>
<img id="captcha" src="<?php echo $this->Html->url('/pages/captcha_image');?>" alt="" />
<pre>

Simple right.

Now if your user doesn’t understand current captcha he will want another one. How you do that?
here is how

 
<a href="javascript:void(0);" 
onclick="javascript:document.images.captcha.src="<?php echo $this->Html->url("/pages/captcha_image");?>?" + Math.round(Math.random(0)*1000)+1" >Reset</a>

To check the captcha in controller we will use this one.


if($this->data['ContactMsge']['captcha']!=$this->Session->read('captcha'))
{
    $this->Session->setFlash(__('Please enter correct captcha code and try again.', true));
}

So as you can see you add this to you site very easily and it is very much customizable. You can download the vendor files along with image and font HERE.


Leave a Reply

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