2021-08-03 11:54:14 1036次浏览 1条回答 0 悬赏 10 金钱

大佬们,请问一下yii自带的验证码可以改为纯数字吗?

public function actions() {
        return [
            'captcha' =>  [
                'class' => 'yii\captcha\CaptchaAction',
                'height' => 50,
                'width' => 80,
                'minLength' => 4,
                'maxLength' => 4
            ],
        ];
    }
补充于 2021-08-03 15:03
namespace yii\captcha;

class   Newcaptcha extends CaptchaAction

{

    protected function generateVerifyCode()

    {

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

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

        }

        if ($this->minLength < 3) {

            $this->minLength = 3;

        }

        if ($this->maxLength > 20) {

            $this->maxLength = 20;

        }

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

 

        $letters = '1234567890123456789012';

        $vowels = 'aeiou';

        $code = '';

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

            if ($i % 2 && mt_rand(0, 10) > 2 || !($i % 2) && mt_rand(0, 10) > 9) {

                $code .= $vowels[mt_rand(0, 4)];

            } else {

                $code .= $letters[mt_rand(0, 20)];

            }

        }

        return $code;

    }

}

生成类文件成功。
然后再更改控制器的配置

'captcha' => [

    'class' => 'yii\captcha\Newcaptcha',

    'maxLength' => 5,

    'minLength' =>5

],

最佳答案

    没有找到数据。
您需要登录后才可以回答。登录 | 立即注册
PHP学院的中学生
副总裁

PHP学院的中学生

注册时间:2018-10-23
最后登录:2024-04-07
在线时长:168小时1分
  • 粉丝29
  • 金钱4730
  • 威望30
  • 积分6710

热门问题