Home  >  Article  >  PHP Framework  >  yii prompts an error when entering the correct verification code

yii prompts an error when entering the correct verification code

王林
王林Original
2019-12-24 13:43:471556browse

yii prompts an error when entering the correct verification code

When making a request, I found that the correct verification code was entered, but the verification code error was prompted.

Finally, following the code, we found that if the Model is validated separately before saving, the verification code will be regenerated after the verification is completed. Then when we Model save, we will also perform validate verification. During verification, the verification code has been regenerated, so it will not match.

Problem discovery:

We can look at framework/web/widgets/captcha/CCaptchaAction.php and we can easily find the problem.

<?php
class CaptchaAction extends CCaptchaAction{
    public function validate($input, $caseSensitive)
    {
        $code = $this->getVerifyCode();
        $valid = $caseSensitive ? ($input === $code) : !strcasecmp($input, $code);
        $session = Yii::app()->session;
        $session->open();
        $name = $this->getSessionKey() . &#39;count&#39;;        
        if (!Yii::app()->request->isAjaxRequest) {
            $session[$name] = $session[$name] + 1;
        }        
        // 这里会重新生成
        if ($session[$name] > $this->testLimit && $this->testLimit > 0) {
                    $this->getVerifyCode(true);
        }
                return $valid;
    }
}
// 如果这里用到了验证码,就会出问题$model = new Test();

$model->validate();

$model->save();
// 这样是正确的$model = new  Test();
// 把需要验证的 attribute 放进去,排除验证码字段
$model->validate(array(&#39;test1&#39;,&#39;test2&#39;));
$model->save()

Recommended related articles and tutorials: yii tutorial

The above is the detailed content of yii prompts an error when entering the correct verification code. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn