How to solve the problem of Yii entering the correct verification code but the verification fails

怪我咯
Release: 2023-03-10 14:14:02
Original
1374 people have browsed it

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();
Copy after login

// 这样是正确的

$model = new Test();
// 把需要验证的 attribute 放进去,排除验证码字段
$model->validate(array('test1','test2'));

$model->save()
Copy after login

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;
 }
}
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!