Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to implement captcha in cakephp ?

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 586
    Comment on it

    A CAPTCHA is a system that ensures sites against bots by producing and evaluating tests that people can pass however current PC programs can't. A CAPTCHA (an acronym for "Completely Automated Public Turing test to tell Computers and Humans Apart") is a kind of test reaction test utilized as a part of registering to figure out if or not the client is human.Here is the simple way to create captcha in cakephp :-

    Step 1:  Make the folder named as captcha in your project->app->Vendor. In that folder, add two folder namely fonts & Images. Also, add captcha.php file and add the following code in it :-

    <?php
    
    class captcha {	
    	public function show_captcha() {
    		if (session_id() == "") {
    			session_name("CAKEPHP");
    			session_start();
    		}
    
    		$path= VENDOR . 'captcha';
    		$imgname = 'noise.jpg';
    		$imgpath  = $path.'/images/'.$imgname;
    		
    		$captchatext = md5(time());
    		$captchatext = substr($captchatext, 0, 5);
    		$_SESSION['captcha']=$captchatext;
    
    		// echo $imgpath; die;
    
    		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;
    		}		 
    	}
    }
    
     
    ?>

    Step 2: Add the following in UsersController.php :-

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

    Step  3: Add the following in login.ctp :-

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

     Step 4: To check the captcha, add the following in UserController.php :-

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

     

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: