When a requirement is made, it is found that the correct code is entered, but the verification code is wrong.
The last trace code found that if the Model was save
previously verified separately, the verification validate
code will be regenerated after the verification is completed.
Then in our Model save
, we will also validate
verify that 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 see framework/web/widgets/captcha/CCaptchaAction.php
that it's easy to find the problem.
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 introduced to solve the Yii input correct verification code verification failure, including the aspects of the content, I hope the PHP tutorial interested in a friend helpful.