本文介绍了yii2 验证码在页面刷新时不会改变的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是 yii2 高级框架的默认验证码实现.我有一个问题:我想在每次刷新页面时更改我的验证码,但是当我刷新页面时,我的验证码没有改变.

I am using the default captcha implementation of the yii2 advanced framework. I have a problem: I want to change my captcha code every time I refresh a page but when I refresh the page my captcha code does not change.

推荐答案

最正确的解决方案是创建你自己的 CaptchaAction,扩展 yii\captcha\CaptchaAction并按如下方式覆盖 run() 方法:

The most correct solution will be to create your own CaptchaAction, that extends yii\captcha\CaptchaAction and override the run() method as follows:

namespace app\actions; // Change to your own

class CaptchaAction extends yii\captcha\CaptchaAction {
    public $autoRegenerate = true;

    public function run()
    {
       if ($this->autoRegenerate && Yii::$app->request->getQueryParam(self::REFRESH_GET_VAR) === null) {
           $this->setHttpHeaders();
           Yii::$app->response->format = Response::FORMAT_RAW;
           return $this->renderImage($this->getVerifyCode(true));
       }
       return parent::run();
    }
}

这篇关于yii2 验证码在页面刷新时不会改变的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-25 12:29