Captcha is a test which can't be pass by computer but it is very easy for the humans to pass it.
For implementing captcha in cakephp we have to follow simple steps.
Step1:- First we have download captcha package from https://github.com/inimist/cakephp-captcha .
Step2:- Then we have to put  all files in this package in  their corresponding folders.
Step3;- Then we have to include all this files 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', 
'field'=>'captcha'));
var $uses = array('CurrentModel');
public $helpers = array('Captcha.Captcha');
Step4:-  Then we have to add this function in our controller
function captcha()  {
    $this->autoRender = false;
    $this->layout='ajax';
    $this->Captcha->create();
}
Step5:-  Put the similar logic to the function which is the "action" of your form, in your controller. 
function add()  {
    if(!empty($this->request->data))    {
        $this->Signup->setCaptcha('captcha', 
    $this->Captcha->getCode('Signup.captcha'));
        $this->Signup->set($this->request->data);
        if($this->Signup->validates())  {
            
        }   else    { 
            $this->Session->setFlash('Data Validation Failure', 'default', 
    array('class' => 
    'cake-error'));
           
        }
    }
}
Step6:- Then we have to Put CaptchaBehaviour in the Model definitions, as following:-
public $actsAs = array(
    'Captcha.Captcha' => array(
        'field' => array('captcha'),
        'error' => 'Incorrect captcha code value'
    )
);
Step7:- Then we have to Include following code in that view document where we need the captcha picture to show up
echo $this->Form->create("Signups");
$this->Captcha->render();
echo $this->Form->submit(__(' Submit ',true));
echo $this->Form->end();
Step8:- At last we have to place below javascript code in some place in our page so it is called appropriately and execute.
<script 
src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></scr
ipt>
<script>
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;
});
</script>
 
                       
                    
0 Comment(s)