Yii添加验证码

添加带验证码的登陆:

1.先在模型modules下的LoginForm.php定义一个存储验证码的变量:public $verfyCode;


2.然后在rules()方法里定义:array('verifyCode', 'captcha', 'allowEmpty'=>!CCaptcha::checkRequirements());

3.然后在对应视图views下的login.php里定义:
    <?php if(CCaptcha::checkRequirements()): ?>
        <div class="row">
                <?php echo $form->labelEx($model,'verifyCode'); ?>
                <div>
                <?php $this->widget('CCaptcha'); ?>
                <?php echo $form->textField($model,'verifyCode'); ?>
                </div>
                <div class="hint">Please enter the letters as they are shown in the image above.
                <br/>Letters are not case-sensitive.</div>
                <?php echo $form->error($model,'verifyCode'); ?>
        </div>
    <?php endif; ?>
4.在login所属的的控制器中加入:
public function actions()
        {
                return array(
                        // captcha action renders the CAPTCHA image displayed on the contact page
                        'captcha'=>array(
                                'class'=>'CCaptchaAction',
                                'backColor'=>0xFFFFFF,
                        ),
                }
原文地址:https://www.cnblogs.com/grimm/p/5414739.html