日期:2014-05-20 浏览次数:20859 次
首先在protected/components 目录下创建一个MathCaptchaAction,重载generateVerifyCode, renderImage等方法:
class MathCaptchaAction extends CCaptchaAction { protected function generateVerifyCode() { return mt_rand((int)$this->minLength, (int)$this->maxLength); } public function renderImage($code) { parent::renderImage($this->getText($code)); } protected function getText($code) { $code=(int)$code; $rand=mt_rand(1,$code-1); $op=mt_rand(0,1); if($op) { return $code-$rand. '+' . $rand; }else { return $code+$rand. '-' . $rand; } } }
public function actions() { return array( 'captcha'=>array( 'class' => 'MathCaptchaAction', 'minLength' => 1, 'maxLength' => 10, )); }