Mohsen Jalalian
Mohsen Jalalian

Reputation: 1130

yii2 captcha not change on page refresh

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.

Upvotes: 4

Views: 4223

Answers (7)

Diana Maria
Diana Maria

Reputation: 1

This worked for me

$(document).ready(function(){
  setTimeout(() => {
    $("#form-captcha-image").click();
  }, 100);  
});

Upvotes: 0

Mohammad Shourabi
Mohammad Shourabi

Reputation: 41

In your controller, just unset the session of captcha:

 session_start();
 unset($_SESSION["__captcha/panel/panel-auth/captcha"]);
 unset($_SESSION["__captcha/panel/panel-auth/captchacount"]);

Upvotes: 0

bhargav3vedi
bhargav3vedi

Reputation: 619

try this

<script>

window.onload = hello;

function hello()
{    
  document.getElementById('loginform-captcha-image').click();
}

</script>

Upvotes: 2

bhargav3vedi
bhargav3vedi

Reputation: 619

because you have set YII_ENV to TEST like this defined('YII_ENV') or define('YII_ENV', 'test'); change it to defined('YII_ENV') or define('YII_ENV', 'prod');

Upvotes: 1

SilverFire
SilverFire

Reputation: 1592

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();
    }
}

Upvotes: 3

Joe Miller
Joe Miller

Reputation: 3893

I found a dirty way round this - simply trigger the click event when the page loads. Add this code at the very end of your view file, after the end of the form;

$js = <<<JS
       $('#loginform-captcha-image').trigger('click');
JS;
$this->registerJs($js, $this::POS_READY);

It's not very pretty, but it works and it's the only way I've found to get aroun d this problem, which has also plagued my own sites.

Upvotes: 1

Double H
Double H

Reputation: 4160

Update Your CaptchaAction as

public function actions()
{
    return [
        'error' => [
            'class' => 'yii\web\ErrorAction',
        ],
        'captcha' => [
            'class' => 'yii\captcha\CaptchaAction',
            'fixedVerifyCode' => null,
        ],
    ];
}

Read Fixed Verify Code

IF fixedVerifyCode if set then captcha is same as value setted in fixedVerifyCode

// code from yii\captcha\CaptchaAction in Yii2

public function getVerifyCode($regenerate = false)
{
    if ($this->fixedVerifyCode !== null) {
        return $this->fixedVerifyCode;
    }

    $session = Yii::$app->getSession();
    $session->open();
    $name = $this->getSessionKey();
    if ($session[$name] === null || $regenerate) {
        $session[$name] = $this->generateVerifyCode();
        $session[$name . 'count'] = 1;
    }

    return $session[$name];
}

Upvotes: -1

Related Questions