This article describes the method of creating verification code in Yii in the form of examples. The specific steps are as follows:
1. Add the following code under SiteController action():
return array( // captcha action renders the CAPTCHA image displayed on the contact page 'captcha'=>array( 'class'=>'CCaptchaAction', 'backColor'=>0xFFFFFF, ), // 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', ), );
2. (1) Add code under LoginForm model rules():
//captche class needed array('verifyCode', 'captcha','allowEmpty'=>!CCaptcha::checkRequirements()),
(2) Add attributes under LoginForm model:
public $verifyCode;
3. Add code under ContactForm model rules():
// verifyCode needs to be entered correctly array('verifyCode', 'captcha', 'allowEmpty'=>!CCaptcha::checkRequirements()),
4. Add code under login view:
<div class="row"> <?php echo $form->labelEx($model,'verifyCode'); ?> <?php $this->widget('CCaptcha'); ?> <?php echo $form->textField($model,'verifyCode'); ?> <?php echo $form->error($model,'verifyCode'); ?> </div>
This example code is only a brief description of the main functions. Readers can further improve the program code according to their own project needs to make its functions more practical.
This article describes the implementation of Yii verification code. It is just a small example of the author's application. It is also available on the Internet. To summarize, I hope to help Yii enthusiasts in need. 1. The author uses user login, so I add the following code to the sitecontroller: public function actions()
{
return array(
// captcha action renders the CAPTCHA image displayed on the contact page
'captcha'=>array(
'class'=>'CCaptchaAction',
'backColor'=>0xFFFFFF, //Background color
'minLength'=>4, // The shortest is 4 digits
'maxLength'=>4, //The longest is 4 digits
'transparent'=>true, //The display is transparent. When this option is turned off, the background color is displayed
),
);
}
2. Add the following code to the form file (view file such as login.php):
< div>
< ?php echo $form->labelEx($model,'verifyCode'); ?>
< div>
< ; ?php $this->widget('CCaptcha'); ?>
< ?php echo $form->textField($model,'verifyCode'); ?>
< / div>
< div>Enter verification code
There are three steps in total. Add one line of code to each layer of controllers, models, and views to achieve it.
The first step is to add
public function actions() {
return array( 'captcha' = >
array(
'class' => 'CCaptchaAction',
'backColor' => 0xF5F5F5,
'transparent'=>true,
'minLength'=> ;4, //The shortest is 4 digits
'maxLength'=>8, //The length is 4 digits
),
);
}
The second step is to add in models As follows:
public $verifyCode; //You must first define
public function rules()
{
return array(
array('verifyCode', 'captcha '),
);
}
The third step is to add the following in views (use small objects here)
beginWidget('CActiveForm')? >
widget('CCaptcha');?>
textField($model,'verifyCode'); ?>
error($model,'verifyCode'); ?>
endWidget(); ?>
?> ;