Home > Article > Backend Development > yii implements creation of verification code instance analysis, yii verification code instance analysis_PHP tutorial
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): 0c77730f95a710e263de4b5e996e889a
44a8922e6607b31d059827e4b04a93b9
3b90ad58d1ab8b2a9a8580f97d4ca40flabelEx($model,'verifyCode'); ?> 076402276aae5dbec7f672f8f4e5cc81
44a8922e6607b31d059827e4b04a93b9
< ; ?php $this->widget('CCaptcha'); ?>
3b90ad58d1ab8b2a9a8580f97d4ca40ftextField($model,'verifyCode'); ?>
ed6b828a1751958f134a511fe41cd994
44a8922e6607b31d059827e4b04a93b9Enter verification code
076402276aae5dbec7f672f8f4e5cc8116b28748ea4df4d9c2150843fecfba68
3b90ad58d1ab8b2a9a8580f97d4ca40ferror($model,'verifyCode'); ? >
950c0e09a208ea6a6c67c615a801fbc6
3. Add the following code to the Loginform model (LoginForm.php), mainly to add attribute fields, otherwise an error will be reported (non-existent attribute) public $username;
public $password;
public $verifyCode;
public $rememberMe;
private $_identity; Through the above operation, we can actually see the verification code, but during the operation we will find that we do not enter it The verification code is still available because we have not specified that verification is required. Add array('verifyCode','required') to LoginForm.php to specify that it is necessary. If we miss the verification code, it will be as shown in the figure below. Display:
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 add the following code to the sitecontroller... The rest of the full text >>
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:
bc7e8dc4447d08177e46026ec921f5e1beginWidget('CActiveForm')? >
26a80da47b56f4831cdf7ae7bb070476widget('CCaptcha');?>
1474dfd8d57635c571d65b829fea6344textField($model,'verifyCode'); ?>
1474dfd8d57635c571d65b829fea6344error($model,'verifyCode'); ?>
26a80da47b56f4831cdf7ae7bb070476endWidget(); ?>
?> ;