Yii的源码包里面是自带有验证码的相关类的,因此在使用验证码的时候无需再加载外部验证码类来助阵了。下面本文将介绍一下如何在项目中加载Yii自带的验证码功能。
具体分三步:
(1)在需要加载验证码的controllers文件里面添加如下代码:
01 |
public function actions(){ |
02 |
return array( |
03 |
‘captcha‘=> array( |
04 |
‘class‘=>‘Captcha‘, |
05 |
‘width‘=>65, //默认120 |
06 |
‘height‘=>25, //默认50 |
07 |
‘padding‘=>0, //文字周边填充大小 |
08 |
‘backColor‘=>0xFFFFFF, //背景颜色 |
09 |
‘foreColor‘=>0x2040A0, //字体颜色 |
10 |
‘minLength‘=>4, //设置最短为4位 |
11 |
‘maxLength‘=>4, //设置最长为4位,生成的code在6-7直接rand了 |
12 |
‘transparent‘=>false, //显示为透明,默认中可以看到为false |
13 |
‘offset‘=>1, //设置字符偏移量 |
14 |
‘testLimit‘=>0 //限制相同验证码出现的次数,0位不限制 |
15 |
) |
16 |
); |
17 |
} |
(2)在controllers文件所对应的models文件里面添加如下代码:
01 |
<?php |
02 |
...... |
03 |
public $verifyCode;//必须先定义 |
04 |
...... |
05 |
public function rules(){ |
06 |
return array( |
07 |
...... |
08 |
//注意这里的‘on‘=>‘login‘,即action=login的时候显示 |
09 |
array(‘verifyCode‘,‘captcha‘,‘on‘=>‘login‘,‘allowEmpty‘=>!extension_loaded(‘gd‘)), |
10 |
); |
11 |
} |
12 |
...... |
13 |
?> |
(3)在需要加载验证码的views页面添加如下代码:
01 |
<?php |
02 |
$this->widget(‘CCaptcha‘, |
03 |
array( |
04 |
‘showRefreshButton‘=>false, |
05 |
‘clickableImage‘=>true, |
06 |
‘imageOptions‘=>array( |
07 |
‘alt‘=>‘点击换图‘, |
08 |
‘title‘=>‘点击换图‘, |
09 |
‘id‘=>‘checkcodeImg‘, |
10 |
‘style‘=>‘cursor:pointer;‘ |
11 |
) |
12 |
) |
13 |
);?> |
原文:http://www.cnblogs.com/xieqian111/p/5212448.html