1、在SiteController中添加如下代码:
/** * Declares class-based actions. */ public function actions() { 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、在 Model/LoginForm.php中
添加一个属性: public $verifyCode;
在rules数组最后添加:
array('verifyCode', 'captcha', 'allowEmpty'=>!CCaptcha::checkRequirements()),
3、在视图中,在view/login.php中需要添加验证码的地方写上:
<input id='LoginForm_verifyCode' type="text" name="LoginForm[verifyCode]" >
<?php $this->widget('CCaptcha'); ?> // 下面这个可以点击图片进行换验证码 <div><?php $this->widget('CCaptcha',array('showRefreshButton'=>false,'clickableImage'=>true,'imageOptions'=>array('alt'=>'点击换图','title'=>'点击换图','style'=>'cursor:pointer'))); ?></div>