Home > Article > Backend Development > How to solve the problem of Yii entering the correct verification code but the verification fails
This article mainly introduces to you the solution to the problem that Yii enters the correct verification code but the verification fails. The article introduces it in detail through the example code, which has certain reference and learning value for everyone. Friends who need it can take a look below. Bar.
Preface
When I was making a request recently, I found that I entered the correct verification code, but it was prompted that the verification code was wrong
Finally, following the code, we found that if the Model was separately verified before saving, then after the verification is completed, the verification code
will be regenerated and then when we Model save
, it will also be performed validate. During verification, the verification code has been regenerated, so it will not match
// 如果这里用到了验证码,就会出问题 $model = new Test(); $model->validate(); $model->save();
// 这样是正确的 $model = new Test(); // 把需要验证的 attribute 放进去,排除验证码字段 $model->validate(array('test1','test2')); $model->save()
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() . 'count'; if (!Yii::app()->request->isAjaxRequest) { $session[$name] = $session[$name] + 1; } // 这里会重新生成 if ($session[$name] > $this->testLimit && $this->testLimit > 0) { $this->getVerifyCode(true); } return $valid; } }
The above is the detailed content of How to solve the problem of Yii entering the correct verification code but the verification fails. For more information, please follow other related articles on the PHP Chinese website!