Monday, 16 February 2015

Implement CAPTCHA in YII

  1. CAPTCHA (an acronym for "Completely Automated Public Turing test to tell Computers and Humans Apart") is a type of challenge-response test used in computing to determine whether or not the user is human.

There is a build in CAPTCHA in yii framework we just needed to use it!! There are many more extensions to implement captcha in yii like "Captcha - extended"

http://www.yiiframework.com/extension/captcha-extended/

Here we are demonstrating about build-in captcha

First of model we have loginform.php in models open that and follow below steps:


In loginform.php add a attribute "verifyCode"  like as follows

class LoginForm extends CFormModel
{
       public $username;                      // This is pre defined
public $password;                     // This is pre defined
public $rememberMe;              // This is pre defined

public $verifyCode;  // This is added by us

private $_identity;                    // This is pre defined

...................................................................
.................................................................
public function rules()
{
return array(
// username and password are required
array('username, password', 'required'),
// rememberMe needs to be a boolean
array('rememberMe', 'boolean'),
// password needs to be authenticated
array('password', 'authenticate'),

  array('verifyCode', 'captcha', 'allowEmpty' => !CCaptcha::checkRequirements(), 'on' => 'withCaptcha'),                 // This is to be added in model rules

);

}
.................................................  // Rest are as it is dont change anything
.................................................
}  // End of Class LoginForm

Now coming to Controller we have SiteController.php

Follow the steps

class SiteController extends Controller
{
/**
* Declares class-based actions.
*/


public function actions()
{
return array(
// captcha action renders the CAPTCHA image displayed on the contact page

  'captcha'=>array(
                'class'=>'CCaptchaAction',
                'backColor'=>0xFFFFFF,
            ),            
                       // The above added to the actions
// page action renders "static" pages stored under 'protected/views/site/pages'
// They can be accessed via: index.php?r=site/page&view=FileName
'page'=>array(
'class'=>'CViewAction',
),
);
}

     ..............................................................
     ..............................................................

    public function actionLogin()
{
$model=new LoginForm;

/* if (Yii::app()->user->getState('attempts-login') > 3) { //make the captcha required if the unsuccessful attemps are more of three
            $model->scenario = 'withCaptcha';
        }  */

               $model->scenario = 'withCaptcha';      
                 // This is to be added for login action to validate captcha


     ..............................................................
     ..............................................................
      }

     .............................................................. // Rest are as it is dont change anything
     ..............................................................
}   // End of site Controller class

Finally the view i.e., site\login.php

add the below code after password field in login form

<div class="row">
                <?php echo $form->labelEx($model, 'verifyCode'); ?>
                <div>
                    <?php // $this->widget('CCaptcha'); ?>
<?php $this->widget('CCaptcha', array('buttonOptions' => array('style' => 'display:block'))); ?>
                    <?php echo $form->textField($model, 'verifyCode'); ?>
                </div>
                <?php echo $form->error($model, 'verifyCode'); ?>
 </div>

so your view should be like this 

.......................................
........................................
<div class="row">
<?php echo $form->labelEx($model,'password'); ?>
<?php echo $form->passwordField($model,'password'); ?>
<?php echo $form->error($model,'password'); ?>
</div>

  <div class="row">
                <?php echo $form->labelEx($model, 'verifyCode'); ?>
                <div>
                    <?php // $this->widget('CCaptcha'); ?>
<?php $this->widget('CCaptcha', array('buttonOptions' => array('style' => 'display:block'))); ?>
                    <?php echo $form->textField($model, 'verifyCode'); ?>
                </div>
                <?php echo $form->error($model, 'verifyCode'); ?>
            </div>
// The above is the added code
<div class="row buttons">
<?php echo CHtml::submitButton('Login'); ?>
</div>
........................................
..........................................

Now Run the application you can find the captcha.. 

No comments:

Post a Comment