1、在controller中加入代码
(1)启用
<?php
public function actions()
{
return array(
// 启用验证码组件
'captcha'=>array(
'class'=>'CCaptchaAction',
'backColor'=>0xFFFFFF,
'maxLength'=>4, // 最多生成几个字符
'minLength'=>4, // 最少生成几个字符
'fixedVerifyCode' => substr(md5(time()),11,4), //每次都刷新验证码
),
);
}
?>
(2)添加进入规则
<?php
array('allow',
'actions'=>array('captcha'),
'users'=>array('*'),
),
?>
2、在model中加入代码
(1)声明
1
2
3
|
<?php
public
$verifyCode
;
?>
|
(2)加入属性
1
2
3
4
5
6
7
8
|
<?php
public
function
attributeLabels()
{
return
array
(
'verifyCode'
=>
'Verification Code'
,
);
}
?>
|
(3)加入过滤规则
1
2
3
|
<?php
array
(
'verifyCode'
,
'captcha'
,
'allowEmpty'
=>!
extension_loaded
(
'gd'
)),
?>
|
3、在view中写代码
1
2
3
4
5
6
7
8
9
10
11
12
|
<?php
if
(CCaptcha::checkRequirements()): ?>
<div
class
=
"row"
>
<?php
echo
$form
->labelEx(
$model
,
'verifyCode'
); ?>
<div>
<?php
$this
->widget(
'CCaptcha'
,
array
(
'showRefreshButton'
=>false,
'clickableImage'
=>true,
'imageOptions'
=>
array
(
'alt'
=>
'点击换图'
,
'title'
=>
'点击换图'
,
'style'
=>
'cursor:pointer'
))); ?>
<?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
; ?>
|