在yii2中刷新浏览器后不更改验证码

在yii2中刷新浏览器后不更改验证码,yii2,captcha,page-refresh,Yii2,Captcha,Page Refresh,我在项目中使用默认验证码,我有两个问题: 1-刷新浏览器后,不要更改验证码 2-当用户在页面上未处于活动状态时,验证码不会过期 我也读过这篇文章,但它帮不了我。因为它在我的代码yi_ENV_TEST中设置为null。并在actionCaptcha类中运行操作,以便在页面刷新期间更改验证码 尽管这些代码是在Yii2中使用Captcha的默认代码,但我还是在这里发布了这些代码 在vendeor\yiisoft\yii2\captcha\CaptchaAction.php中 class Captcha

我在项目中使用默认验证码,我有两个问题:

1-刷新浏览器后,不要更改验证码

2-当用户在页面上未处于活动状态时,验证码不会过期

我也读过这篇文章,但它帮不了我。因为它在我的代码yi_ENV_TEST中设置为null。并在actionCaptcha类中运行操作,以便在页面刷新期间更改验证码

尽管这些代码是在Yii2中使用Captcha的默认代码,但我还是在这里发布了这些代码

在vendeor\yiisoft\yii2\captcha\CaptchaAction.php中

class CaptchaAction extends Action
{

    const REFRESH_GET_VAR = 'refresh';

 .
 .
 .

    public function run()
    {

        if (Yii::$app->request->getQueryParam(self::REFRESH_GET_VAR) !== null) {

            // AJAX request for regenerating code
            $code = $this->getVerifyCode(true);
            Yii::$app->response->format = Response::FORMAT_JSON;
            return [
                'hash1' => $this->generateValidationHash($code),
                'hash2' => $this->generateValidationHash(strtolower($code)),
                // we add a random 'v' parameter so that FireFox can refresh the image
                // when src attribute of image tag is changed
                'url' => Url::to([$this->id, 'v' => uniqid('', true)]),
            ];
        }

        $this->setHttpHeaders();
        Yii::$app->response->format = Response::FORMAT_RAW;

        return $this->renderImage($this->getVerifyCode());
    }

 .
 .
 .


}
在controller/sitecontoller.php中

 /**
     * {@inheritdoc}
     */
    public function actions() {

        return [
            'error' => [
                'class' => 'yii\web\ErrorAction',
            ],
            'captcha' => [
                'class' => 'yii\captcha\CaptchaAction',
                'fixedVerifyCode' => YII_ENV_TEST ? 'testme' : null,
            ],
        ];
    }
在我看来

<?php
$form = ActiveForm::begin([
            'id' => 'login-form',
            'enableAjaxValidation' => true,
            'enableClientValidation' => false,
            'validateOnBlur' => false,
            'validateOnType' => false,
            'validateOnChange' => false,
        ])
?>
 <?=
                $form->field($model, 'captcha')->widget(Captcha::className(), [
                    'captchaAction' => ['/site/captcha']])
                ?>
....

....

推荐

此选项不可用,如果希望每次刷新时都更改captcha,则可以使用扩展captchaAction类创建自己的capcha类

不推荐


但是,如果您创建javascript代码,当页面刷新时单击验证码图像,您就可以实现您想要的功能,这将非常有用。

在siteController集合中
verifyCode=>true
@mohsen,当我在siteController中添加verifyCode=>true时。不要加载我的验证码,我在运行操作中添加verifyCode=>true,在fixedVerifyCode之后:这是正确的吗?公共函数操作(){return['error'=>['class'=>'yii\web\ErrorAction',],'captcha'=>['class'=>'yii\captcha\CaptchaAction','fixedviryCode'=>yii\u ENV\u TEST?'testme':null,'verifyCode'=>true,],];}我将run action(CaptchaAction)中的$this->getVerifyCode()更改为$this->getVerifyCode(true)。但我不知道如何使captch过期,在长时间闲置之后,这是您需要的吗?如果您需要创建自己的capcha类,正如我在第二种方法(不推荐)中所说的,您可以使用javascript处理。它的客户端问题将第二种方法放到javascript中的计时器上