验证码改为四位数字

群中有人问起,顺便贴过来

class MyCaptchaAction extends CCaptchaAction {

    const SESSION_VAR_PREFIX='Ext.MyCaptchaAction.';

    public $minLength=4; //默认四位

    public $maxLength=4; //默认四位

    protected function generateVerifyCode()

{


	if($this->minLength<3)


		$this->minLength=3;


	if($this->maxLength>20)


		$this->maxLength=20;


	if($this->minLength>$this->maxLength)


		$this->maxLength=$this->minLength;


	$length=rand($this->minLength,$this->maxLength);





	$letters='1234567890';


	$code='';


	for($i=0;$i<$length;++$i)


	{

                    $code.=$letters[rand(0,10)];

	}





	return $code;


}

}

放到 protected/extensions 目录

在配置文件 protected/config/main.php中自动加载 extensions 目录的类

    'import'=>array(

        'application.models.*',

        'application.components.*',

        'application.extensions.*',

    ),

使用:

'captcha'=>array(

  'class'=>'application.extensions.MyCaptchaAction',

),

不错!

要是能直接有参数可以配置就好了。