jtt416
- jtt416 评论了教程 解决Yii2中刷新网页时验证码不刷新的问题
我也发个吧
在PHP中public function actions() { return array( 'captcha' => array( 'class' => 'yii\captcha\CaptchaAction', 'maxLength' => 5, 'minLength' => 5, 'width'=>120, 'height'=>'27', ), ); }
这个大家都知道,在页面中我没用YII的form,而是自己写的表单
<input type="text" id="codeid" name="checkcode" class="form-control" placeholder="验证码"> <div class="input-group-addon" id="logincode"> <img src="/res/images/logincode.gif" id="code_img" alt="点击刷新"> </div>
且看JS部分
<script> $(function(){ $("#codeid").focus(function(){ $.get('<?php echo Yii::$app->urlManager->createUrl('site/captcha');?>?',{refresh:1},function(data){ evaldata=eval(data); $("#code_img").attr('src',evaldata.url); }); }); $("#code_img").click(function(){ $.get('<?php echo Yii::$app->urlManager->createUrl('site/captcha');?>',{refresh:1},function(data){ evaldata=eval(data); $("#code_img").attr('src',evaldata.url); }); }); }) </script>
我们通过向/site/captcha?refresh=1发送GET请求,得到的数据如下,
{"hash1":538,"hash2":538,"url":"/backend/web/site/captcha?v=56a8243edb9a8"}
很清楚的我们得到了一个新的验证码URL,
这里说下V,V是用PHP的uniqid()生成的一个唯一码
这个方法不用改YII的任何代码哦如果你在model里面进行两次验证码的验证那么你的验证就会通不过哦。
YII的验证码不论是生成的时候还是验证的时候都是通过了getVerifyCode,验证通过后会重新在生成一次验证码,所以在model里如果你调用了两次验证的话,就通不过哦
- jtt416 评论了教程 解决Yii2中刷新网页时验证码不刷新的问题
支持大家解决问题