Analysis of yii Implementation to Create Verification Code Instance
- 2021-07-10 19:15:26
- OfStack
In this paper, the method of creating verification code by yii is described as an example. 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, and readers can further improve the program code according to their own project requirements, so that its functions are more practical.