【问题标题】:Does not change captcha aftre refresh browser in yii2在 yii2 中刷新浏览器后不会更改验证码
【发布时间】:2020-01-15 08:41:26
【问题描述】:

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

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

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

我也读过这个link 但它对我没有帮助。因为它在我的代码 YII_ENV_TEST 中设置为空。并在 actionCaptcha 类中运行操作有一种方法可以在页面刷新期间更改验证码显然!!。

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

在供应商\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());
    }

 .
 .
 .


}

在控制器/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']])
                ?>
....

【问题讨论】:

  • 在站点控制器中设置verifyCode =&gt; true
  • @mohsen 当我在 siteController 添加 verifyCode => true 时。不要加载我的验证码,我在 runActions 中添加 verifyCode => true ,在 fixedVerifyCode 之后。 : 它是否正确? public function actions() { return [ 'error' => [ 'class' => 'yii\web\ErrorAction', ], 'captcha' => [ 'class' => 'yii\captcha\CaptchaAction', 'fixedVerifyCode ' => YII_ENV_TEST ? 'testme' : null, 'verifyCode' => true, ], ]; }

标签: yii2 captcha page-refresh


【解决方案1】:

According to samdark in this url

推荐

此选项不可用,如果您希望验证码在每次刷新时都发生变化,您可以创建自己的验证码类并扩展验证码操作类

不推荐

但如果您创建 javascript 代码,当页面刷新点击验证码图像时,您可以实现您想要的,这会很有帮助。

【讨论】:

  • 我将运行动作中的 $this->getVerifyCode() ,(CaptchaAction) 更改为 $this->getVerifyCode(true)。但是页面闲置了很长时间后,我不知道如何使验证码过期
  • 这是你需要的吗?如果您需要创建自己的验证码类
  • 正如我在第二种方式中所说的(不推荐),您可以使用 javascript 来处理。它的客户端问题把第二种方法放在javascript中
猜你喜欢
  • 2015-04-03
  • 2016-12-21
  • 2011-07-29
  • 2017-12-06
  • 1970-01-01
  • 2019-06-23
  • 1970-01-01
  • 2013-10-17
  • 2014-07-12
相关资源
最近更新 更多