Yii's source code package has related classes with its own verification code, so there is no need to load external ones when using the verification code The CAPTCHA class comes to help. The following article will introduce how to load the verification code function that comes with Yii in the project.
It is divided into three steps:
(1) Add the following code in the controllers file that needs to load the verification code:
public function actions(){ return array( 'captcha'=> array( 'class'=>'Captcha', 'width'=>65, //默认120 'height'=>25, //默认50 'padding'=>0, //文字周边填充大小 'backColor'=>0xFFFFFF, //背景颜色 'foreColor'=>0x2040A0, //字体颜色 'minLength'=>4, //设置最短为4位 'maxLength'=>4, //设置最长为4位,生成的code在6-7直接rand了 'transparent'=>false, //显示为透明,默认中可以看到为false 'offset'=>1, //设置字符偏移量 'testLimit'=>0 //限制相同验证码出现的次数,0位不限制 ) ); }
(2) Add the following code in the models file corresponding to the controllers file:
<?php ...... public $verifyCode;//必须先定义 ...... public function rules(){ return array( ...... //注意这里的'on'=>'login',即action=login的时候显示 array('verifyCode','captcha','on'=>'login','allowEmpty'=>!extension_loaded('gd')), ); } ...... ?>
(3) Add the following code to the views page that needs to load the verification code:
<?php $this->widget('CCaptcha', array( 'showRefreshButton'=>false, 'clickableImage'=>true, 'imageOptions'=>array( 'alt'=>'点击换图', 'title'=>'点击换图', 'id'=>'checkcodeImg', 'style'=>'cursor:pointer;' ) ) );?>