Yii2解决验证码点击、刷新页面不刷新问题解决
原创链接到原创文章。链接到原始文章。原始链接。 https://www.jianshu.com/p/7455f270ebcb
首先,解决了不刷新验证码就刷新页面的问题
通过分析源代码,我们只需要修改 yiicaptchaCaptchaAction
的 run()
方法在方法中调用时在方法中调用 getVerifyCode(true) 然后问题可以解决,但不能修改源代码,然后可以采取继承和重载的方法来实现它。
-
新建
backend/components/CaptchaAction.php
<?php namespace backendcomponents;
use yiiwebResponse;
class CaptchaAction extends yiicaptchaCaptchaAction {
/** * 默认验证码刷新页面不会自动刷新 */ public function run() { $this->setHttpHeaders(); Yii::$app->response->format = Response::FORMAT_RAW; return $this->renderImage($this->getVerifyCode(true)); }
}
-
在 SiteController 控制器寄存器中的注册控制器中的控制器寄存器中的注册 CaptchaAction 方法:
public function actions() { return [ //默认验证码刷新页面不会自动刷新 captcha => [ class => backendcomponentsCaptchaAction, testLimit => 1, maxLength => 6, minLength => 6, padding => 1, height => 50, width => 140, offset => 1, ], ]; }
然后解决点击验证码笔刷的问题
只需标记验证码图像只需标记验证码图像 添加 onclick => this.src=this.src+"&c="+Math.random();
事实上,在其他框架中也是如此。
= yiicaptchaCaptcha::widget([
name=>captchaimg,
captchaAction=>site/captcha,
imageOptions=>[
id=>captchaimg,
title=>换一个,
alt=>换一个,
style=>cursor:pointer;,
// 添加点击事件添加点击事件
onclick => this.src=this.src+"&c="+Math.random();
],
template=>{image}
]);?>
如何验证提交的验证码是否正确?在本例中,登录表单提交给 site/login,处理过程如下。治疗方法如下。处理过程如下。处理过程如下。
/*简单示例*/
public function actionLogin(){
$post = Yii::$app->resquest->post();
if (!$this->createAction(captcha)->validate($post[CAPTCHA], false)) {
//验证码错误消息验证码错误警报验证码错误提示验证码错误指示
}
}
版权声明
所有资源都来源于爬虫采集,如有侵权请联系我们,我们将立即删除